Reputation: 569
I have a collection in mongodb named post.I want to extract all the post id which are not disabled from that collection.I have used jagi astronomy a package of meteor js to create the schema.When i am using the code:-
let post=Post.findOne({'disabled':false});
console.log(post._id);
In the above case it shows the id of only one post not of all.
While using
let post=Post.find({'disabled':false});
console.log(post._id)
It is showing undefined. Please help!
Upvotes: 1
Views: 60
Reputation: 103425
You are getting undefined because find()
returns a cursor. It does not immediately access the database or return documents. Cursors provide fetch()
to return all matching documents, map()
and forEach()
to iterate over all matching documents, and observe
and observeChanges
to register callbacks when the set of matching documents changes.
In this case you need to call fetch()
on the cursor to all matching documents as an Array i.e.
let posts=Post.find({'disabled':false}).fetch();
console.log(posts[0]._id) // log the first element in the results set
or using forEach()
to iterate the cursor:
Post.find({'disabled':false}).forEach(post => console.log(post._id));
Upvotes: 1