MongoDB如何使用find的结果保存到另一个集合中

时间:2014-03-24 20:35:30

标签: node.js mongodb mongoose

这是我的Mongoose Schema:

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

你能帮我弄清楚我做错了吗?

1 个答案:

答案 0 :(得分: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"
      ]
    }
  ]
}

将代码与您已经或正在采取的措施进行比较以发现差异。