私はのような配列を持ってい[ 570dec75cf30bf4c09679deb, 56fe44836ce2226431f5388f ]
ます。今、私は次のnotifications
ような名前の新しいコレクションにこれを挿入したい
{
"_id": ObjectId("57bd39c9734ff3602a312b33"),
"userId": ObjectId("570dec75cf30bf4c09679deb")
}
and
{
"_id": ObjectId("57bd39c8734ff3602a312b32"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}
これを挿入するためにnode.jsにクエリを記述しましたが、配列の最後の要素をこのように2回挿入します
{
"_id": ObjectId("57bd39c9734ff3602a312b33"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}
and
{
"_id": ObjectId("57bd39c8734ff3602a312b32"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}
私がこのように書いたクエリ
router.post('/creatList', function(req,res){
console.log(req.body.email);
var emails = req.body.email;
if(req.body.wData.wishListType == 'Shared'){
var findUsers = function(db, callback) {
var cursor;
cursor = db.collection('users').find({email: { $in: emails }})
cursor.toArray(function(err, docs){
if(err){
callback(new Error("Some problem"));
} else {
callback(null,docs);
}
});
};
MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
findUsers(db, function(err,docs) {
db.close();
console.log(docs);
for(var key in docs){
console.log(key);
var ids = docs[key]._id;
console.log(ids);
var insertDocument = function(db, callback) {
db.collection('notifications').insert({
"userId" : ids,
},function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the notifications collection.");
callback();
});
};
MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
db.close();
});
});
}
});
});
});
基本的に、users.find()からの結果を再マップして、通知コレクションのスキーマと一致させる必要があります。これは機能するはずです:
var docsToInsert = docs.map(function (doc) {
// this assumes your users collection has an _id that you actually want to use as the reference for you notifications.userId field
// actually, you probably want "return {"userId": ObjectID(doc._id)};" since you are using the native mongodb api
return { "userId": doc._id };
});
したがって、残りの部分を少し単純化すると、次のようになります。
router.post('/creatList', function (req, res) {
console.log(req.body.email);
var emails = req.body.email;
if (req.body.wData.wishListType == 'Shared') {
var findUsers = function (db, callback) {
var cursor;
cursor = db.collection('users').find({ email: { $in: emails } });
cursor.toArray(function (err, docs) {
if (err) {
callback(new Error("Some problem"));
} else {
callback(null, docs);
}
});
};
MongoClient.connect(config.database, function (err, db) {
assert.equal(null, err);
findUsers(db, function (err, docs) {
db.close();
console.log(docs);
var docsToInsert = docs.map(function (doc) {
// this assumes your users collection has an _id that you actually want to use as the reference for you notifications.userId field
// actually, you probably want "return {"userId": ObjectID(doc._id)};" since you are using the native mongodb api
return { "userId": doc._id };
});
MongoClient.connect(config.database, function (err, db) {
assert.equal(null, err);
db.collection("notifications").insert(docsToInsert, function (err, result) {
if (err) {
// handle the err however you like
throw err;
}
db.close();
});
});
});
});
}
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加