2014-10-01 59 views
0

我想寫一個mongo查詢來更新一個集合。

在SQL它會是這個樣子:

UPDATE myCollection SET count=(count-1) 
WHERE otherCollectionId=otherCollection._id 
    AND otherCollectionUserId='HFDDEE78DFDSER34' 
    AND count > 0 

我已經嘗試了一些事情,但是這個人是絕對行不通的。我發現了錯誤:

MongoError: Invalid modifier specified $gt

我蒙戈查詢至今:

myCollection.update({otherCollectionId: otherCollection._id, 
        otherCollectionUserId: Meteor.userId() 
        }, 
        {$gt: {count: 0}, 
        $inc: {count: -1} 
        }); 

任何想法?

回答

1

我想這可能是你想要什麼:

myCollection.update(
    { 
    otherCollectionId: otherCollection._id, 
    otherCollectionUserId: Meteor.userId(), 
    count: {$gt: 0} 
    }, 
    {$inc: {count: -1}}, 
    {multi: true} 
); 

選擇的所有三個部分AND組合在一起,然後我們增加count -1,最後查詢將運行在多個文件(不只是第一個匹配選擇器)。

+0

這樣做的技巧,謝謝:) – user1532669 2014-10-01 00:55:49