Help with accessing arrays in documents


I was wondering if someone can point me in the right direction. I have a collection with an array of objects when I meteor call Collection.findOne( {_id: Id}, {owners_list: {$elemMatch: {userId: currentUserId}}} ) i’m getting different results when i call it from meteor vs running it on Robomongo. In robomongo it returns just the id with out the array but in meteor it returns the whole document. Also what I’m trying to do is check if the current user exist in that array of objects where i’m storing userids. So if I try to print out the array in console I see [object Object]. How do iterate over the array check to see if the currentuser exist in that array. I’m coming from a Java background so pure javascript is new to me. If anyone can help I would surely appreciate it. If I can access [object Object] than I wouldn’t even need to make a meteor call because I can grab if from the templates data context. Unfortunately I dont know how to cast the [object Object] and read what in there.


You can try using console.dir() instead of console.log(), which will output a more detailed tree-like view of the object and its children.


Meteor’s and Mongo’s query API is like: Collection.findOne(,), you’re query should be:

Collection.findOne({_id: Id, ownerslist: {$elemMatch: {userId: currentUserId}}})

If I’m not wrong, that query should work for both, meteor, and mongo.

If you also want to filter out some objects (more details on

Collection.findOne({_id: Id, ownerslist: {$elemMatch: {userId: currentUserId}}}, {fields: {owenerslist: 1}});
// Mongo
db.collection.findOne({_id: Id, ownerslist: {$elemMatch: {userId: currentUserId}}}, {owenerslist: 1});

Something else that helped me on my early days with javascript, was .toString() and JSON.stringify(). but, as @p4bloch mentioned, there are other console.* methods that are also very helpful.


Thanks @pcastrom I looked at it and had my query structured incorrectly.