2016-06-26 47 views
0

我有一個名爲books的集合,其中包含對象 - isbn,作者,書籍數量(num),發佈者等。 剛剛從我的視圖中獲取圖書信息之後,我想檢查圖書的isbn是否已經存在,如果是的話,我想增加書的數量。然而,即使它是同一本書,這個數字也不會增加。有人知道爲什麼NodeJS MongoDb查找重複值並修改增量

var collection = db.collection('books'); 

     var item=collection.findAndModify({ 
     query: { isbn: req.body.isbn13 }, 
     update: { $inc: { num: 1 } }, 
     }); 
     if(item==null) 
     {add the info to the database} 

回答

0

您應該使用一個回調函數來檢查,如果該項目爲空或沒有找到,

https://mongodb.github.io/node-mongodb-native/markdown-docs/insert.html

var collection = db.collection('books'); 

     var item=collection.findAndModify({ 
     query: { isbn: req.body.isbn13 }, 
     update: { $inc: { num: 1 } }, 
     }, function(err, object) 
{ 
//check here & add the info to the database 
});