1

我想保存 Tower obj 和 flat obj。塔式架构

var TowerSchema = new Schema({
name:String,
project:{type:mongoose.Schema.ObjectId, ref:"Project"},
total_unit:Number,
sold_unit:Number,
flats:[{type:mongoose.Schema.ObjectId, title:String, ref:'Flat'}],
createdAt: { type: Date, 'default': Date.now }
});

平面模式

var flatSchema = new Schema({
year: Number,
title:String
})

我正在尝试用 mongoose save 来保存它

var flats1 = new sampleModel({
        title: objTitle,
    });

    flats1.save(function (err) {
        if (err) { 
            console.log('msg: '+err)
        } else {
            console.log('msg: '+flats1)
        }
    });

   var objId = flats1.objectId
    var objTitle = flats1.title

    var newTower = new TowerModel({
        name:towerName,
        project:null,
        total_unit:totalUnit,
        sold_unit:soldUnit,
        flats: [{objId , objTitle }],
        createdAt: { type: Date, 'default': Date.now }
    });

我的输出应该是这样的

{
    "_id" : ObjectId("56bc5da2dc4a3d1c1b00000f"),
    "name" : "sample name five",
    "project" : null,
    "total_unit" : 3,
    "sold_unit" : 3,
    "createdAt" : ISODate("2016-02-11T10:08:34.309Z"),
    "flats" : [ 
       "title":sample,
       "_id":"flats id"
],
    "__v" : 0
}

我是 mongoose 和 Node 的初学者。请在这里帮助我。

4

0 回答 0