簡體   English   中英

如何從流星集合中獲取文檔?

[英]How to get a doc from a collection in meteor?

我對mongodb完全沒有經驗,所以我問如何從流星集合中檢索文檔。 我檢查是否有該用戶的文檔,並用一個對象更新它

            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())

我想通過console.log獲得“保存”對象,但是現在,如果我不使用fetch() (顯然輸出光標對象),它們都將不輸出任何內容或[object Object]

如您所述,Collection.find()返回一個游標 ,而Collection.find()。fetch()返回該文檔。

我不確定我能確切地理解您要做什么,但是也許是這樣(未經測試):

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 } });
}); 

您將整個過程包裝在Meteor方法中,以利用延遲補償,並確保Collection.update具有寫權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM