express.jsに基づいてNode rest APIを構築しようとしています。MongoDBは存在する場合は多くを更新し、存在しない場合は作成します。ノードAPI
私は機能しているポスト機能を持っていますが、もっと必要なものがあります。 は、今の私は、ポストマンを使用してデータを送信し、私のポストの要求は、次のようになります
[
{ "id": 1, "name": "test", "points": 1},
{ "id": 2, "name": "test2", "points": 2},
{ "id": 3, "name": "test3", "points": 32},
{ "id": 4, "name": "test4", "points": 423},
{ "id": 5, "name": "test5", "points": 321}
]
をそして、これは私のポスト機能である:
.post(function(req, res) {
User.insertMany(req.body)
.then(function(mongooseDocuments) {
res.json({ message: 'User created!' });
})
.catch(function(err) {
/* Error handling */
});
})
しかし、今、私はユーザーのためのポイント数を更新したい方私は管理
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
id: Number,
name: String,
points: Number
})
module.exports = mongoose.model('User', UserSchema);
:私はこのスキーマに基づいてユーザを作成したいと思い存在しないデータベース内にあるとid場合(各ユーザーが独自のIDを持っているので、私は_idを使用することはできません) findOneAndUpdateを使用して1人のユーザーを更新しますが、要素の配列を送信しています。複数の要素をどうしたらよいか分かりません。
では、一度に1つの操作を反復して行うことができ、あなたに
可能な重複は(http://stackoverflow.com/questions/25285232/bulk-upsert-in-mongodb-using-mongoose) –