2014-03-24 32 views
0

这是我的猫鼬架构:MongoDB中如何使用查找的结果保存到另一个集合

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": [] 
       } 
      ] 
     } 

你能帮我找出我做错了吗?

+0

不确定你的意思是“打印'c”“。尝试'console.log(JSON.stringify(c,undefined,2));'。代码“已发布”应该可以正常工作。因此,打印错误或者与“打印”有关。 –

+0

本来我是通过'c.save(...)'保存的,并且我得到了与“打印”相同的值。目前,我使用express.js response.send来查看浏览器中c的值。 – faisal

回答

0

这是一个可用的列表。所以,你可能有差异:

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" 
     ] 
    } 
    ] 
} 

代码进行比较,以你所拥有的或正在做现货的差异。

+0

谢谢尼尔,我会检查这个,如果我能解决这个问题,我会接受这个答案。 – faisal

+0

嘿尼尔,我发现了这个问题。我试图保存的对象是另一个findByIdAndUpdate调用的结果。你可以用这个更新你的答案,我会接受它:“var mynetwork_obj = mynetwork.toObject();然后使用新的连接({”users“:mynetwork_obj.user_list});” – faisal

相关问题