简体   繁体   中英

How to get a doc from a collection in meteor?

I am absolutely unexperienced with mongodb, so I am asking how to retrieve a document from a meteor collection. I check if there is a doc for the user and update it with an object

            if (Saves.find({_id: Meteor.userId()}).fetch()) {

                    console.log("Before " +Saves.find({_id: Meteor.userId()}).fetch())
                    if (Meteor.isServer){
                    Saves.update( {_id: Meteor.userId(), save: save} )
                    }
                    console.log("Success " + Saves.find({_id: Meteor.userId()}).fetch())

I want to get that "save" object via console.log , but right now they all output nothing or [object Object] if I don't use fetch() (outputting a cursor object obviously).

As you noted, Collection.find() returns a cursor , whereas Collection.find().fetch() returns the document.

I'm not sure I understand what you're trying to do exactly, but maybe something like this (untested):

var cursor = Saves.find({ _id : Meteor.userId() /* , save : { $not : true } */ });

cursor.forEach(function(item) {
    console.log(item);
    Saves.update({ _id : item._id }, { $set : { save : true } });
}); 

You'd wrap the whole thing in a Meteor method to take advantage of latency compensation, and ensure write permission with your Collection.update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM