Meteor.users.findOne() selector help


#1

I have a SimpleSchema attached to Meteor.users with the following property:

tags: {
    type: [Object],
    label: "Tags",
    optional: true,   
    }
  },
  'tags.$.name' : {
    type: String,
  },
  'tags.$.correct' : {
    type: Number,
  },
  'tags.$.wrong' : {
    type: Number,
  },

And I have created a user with a single “Tag” object

"tags" : [
		{
			"name" : "History",
			"correct" : 0,
			"wrong" : 1
		},
	]

But when I make the call

Meteor.users.findOne({
        'tags.$.name' : "History"
      });

It returns undefined. Does anyone know what I am doing wrong?

Thank you so much for helping. This has been stalling me for days. =\


#2

I believe

Meteor.users.findOne({
    "name" : "History"
});

should work.


#3

No, ‘name’ is a property of each tag object in the array of tags not a property of each user object. And just to check, I tried it and it still didn’t work.


#4

Maybe $elemMatch ?


#5

Try this:

Meteor.users.findOne({tags:{$elemMatch:{"name": "History"}}});


#6

I read about $elemMatch but according to the documentation:

If you specify a single query predicate in the $elemMatch expression, $elemMatch is not necessary.

Nonetheless, I gave it a try. And nope. Still returning undefined.

I just realized that I wasn’t actually attaching the User schema to the Meteor.users collection. But now that I have, there are more errors. I will report back after giving it another try.