2016-06-26 3 views
0

私は、オブジェクト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
は商品がnullまたは見つからないかどうかを確認するには、コールバック関数を使用する必要があり

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 
}); 
関連する問題