[英]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.