[英]MongoDB how to use the result of find to save to another collection
这是我的猫鼬模式:
var ConnectionSchema = new Schema({
users: [{
social_id : String,
name : String,
hair_color : String,
gender : String,
interested_in : [String],
current_look : {
photo_url : String,
identifier: {
type : String,
brand : String,
color : String
}
}
}],
time : {type: Date, "default": Date.now}
});
我正在这样做:
mongoose.model('connection', ConnectionSchema);
var Connection = mongoose.model('connection');
var c = new Connection({
"users": mynetwork.user_list});
其中mynetwork
是另一个findByIdAndUpdate
调用的结果。
当我打印c
我得到这个:
{
"_id": "53308c83b1cd1b081df7a7c4",
"time": "2014-03-24T19:50:27.915Z",
"users": [
{
"_id": "533073ecb3ce5208062a8668",
"interested_in": []
},
{
"_id": "533073ecb3ce5208062a8668",
"interested_in": []
}
]
}
您能帮我弄清楚我做错了什么吗?
这是一个有效的清单。 因此,您可能会有所不同:
var mongoose = require('mongoose');
var Schema = require('mongoose').Schema;
var conn = mongoose.createConnection('mongodb://localhost');
var ConnectionSchema = new Schema({
users: [{
social_id: String,
name: String,
hair_color: String,
interested_in: [String],
current_look: {
photo_url: String,
identifier: {
type: String,
brand: String,
color: String
}
}
}],
time: { type: Date, "default": Date.now }
});
var Connection = conn.model( "connection", ConnectionSchema );
conn.on('open', function() {
var c = new Connection({
users:[{
"interested_in": [
"male",
"female"
],
"current_look": {
"photo_url": "some url"
},
"social_id": "Facebook:524934406",
"gender": "male",
"hair_color": "black",
"name": "Faisal"
}]
});
console.log(
"Created:\n===\n\n" +
JSON.stringify( c, undefined, 2 )
);
c.save(function(err,doc){
if (err) console.log(err);
console.log(
"Saved:\n===\n\n" +
JSON.stringify( doc, undefined, 2 )
);
});
});
这将产生如下输出:
Created:
===
{
"_id": "5330cae8c3b89719766fb529",
"time": "2014-03-25T00:16:40.122Z",
"users": [
{
"social_id": "Facebook:524934406",
"hair_color": "black",
"name": "Faisal",
"_id": "5330cae8c3b89719766fb52a",
"current_look": {
"photo_url": "some url"
},
"interested_in": [
"male",
"female"
]
}
]
}
Saved:
===
{
"__v": 0,
"_id": "5330cae8c3b89719766fb529",
"time": "2014-03-25T00:16:40.122Z",
"users": [
{
"social_id": "Facebook:524934406",
"hair_color": "black",
"name": "Faisal",
"_id": "5330cae8c3b89719766fb52a",
"current_look": {
"photo_url": "some url"
},
"interested_in": [
"male",
"female"
]
}
]
}
将代码与您已有或正在做的代码进行比较以发现差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.