2017-06-17 52 views
0

我想對mongo數據庫集合運行更新查詢,並且要做到這一點,我需要存儲在數據庫中的用戶的ID。這是我的插入代碼:收集插入的返回ID

if(userAccounts.find().count()==0){ 
    userAccounts.insert({ 
    username:Meteor.user().username, 
    likedPosts: [{ 
    postId: [this._id], 
    createdAt: new Date() 
}] 
    }); 

要運行更新查詢,我需要userAccounts數據庫中的id。所以,我需要這樣的東西:

var id=userAccounts(query); // And this should return me the id 
+0

來自同一集合?你問如何只返回'_id'值?並通過什麼查詢?對於''用戶名''? –

+0

我只需要_id值。 Idk什麼查詢,這就是爲什麼我問我只想smth,只返回從userAccounts集合_id。 – Roberto

+0

像'.findOne()。fetch()._ id'? –

回答

0

.insert()返回插入的文件(如果成功的話)的_id

const _id = userAccounts.insert({ 
    username: Meteor.user().username, 
    likedPosts: [{ 
    postId: [this._id], 
    createdAt: new Date() 
    }] 
});