gpt4 book ai didi

angularjs - 如何使用node.js将数组插入mongodb

转载 作者:太空宇宙 更新时间:2023-11-03 23:30:15 24 4
gpt4 key购买 nike

我有一个类似 [ 570dec75cf30bf4c09679deb, 56fe44836ce2226431f5388f ] 的数组。现在我想将其插入名为 notifications 的新集合,例如

{
"_id": ObjectId("57bd39c9734ff3602a312b33"),
"userId": ObjectId("570dec75cf30bf4c09679deb")
}

and

{
"_id": ObjectId("57bd39c8734ff3602a312b32"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}

我在node.js中编写了一个查询来插入它,但它像这样插入数组的最后一个元素两次

{
"_id": ObjectId("57bd39c9734ff3602a312b33"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}

and

{
"_id": ObjectId("57bd39c8734ff3602a312b32"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}

我这样写的查询

app.js

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();
});
});
});
});
}
});

关于angularjs - 如何使用node.js将数组插入mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39115699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com