Mongoose保存数组而不是单个数组

时间:2017-11-25 00:26:17

标签: javascript node.js mongodb mongoose

我正在尝试使用mongoose在MongoDB上保存数组。这是我的模特:

let itemFields = { "name": { "type": "String", "required": true, "unique": true } };

let modelFields = {
    "name": { "type": "String", "required": true, "unique": true },
    "description": { "type": "String" },
    "options": { "type": "Array", "ref": "SelectOptionItem" },
    "company_id": { "type": "ObjectId", "ref": "Company", "required": true },
    "deleted": { "type": "Boolean", "required": true },
    "createdAt": { "type": "Number" },
    "updatedAt": { "type": "Number" }
};

const SelectOptionItem = new mongoose.Schema(itemFields);
const schema = new mongoose.Schema(modelFields);

稍后我有以下代码来保存数据:

schema.statics.create = async function(context, data) {
    console.log("Saved data:");
    console.log(data);
    let so = new this(data);
    so.save();
}

我在哪里获得以下日志:

 Saved data:
 { name: 'aa',
   description: 'aa',
   options: [ { name: '1' }, { name: '2' } ],
   company_id: '59b2cd9a072e4f28b839aaa0',
   deleted: false,
   createdAt: 1511569192524 }

最后,这是我在mongo控制台中获得的内容:

> db.selectoptions.find({});
{ "_id" : ObjectId("5a18b7288c688033a4adcb0c"), "name" : "aa", "description" : "aa", "company_id" : ObjectId("59b2cd9a072e4f28b839aaa0"), "deleted" : false, "createdAt" : 1511569192524, "options" : [ [ { "name" : "1" } ], [ { "name" : "2" } ] ], "__v" : 0 }

修复options是一个数组数组([[]])。后来检索那些数据时我遇到了问题,因为我期待一个阵列。

我需要解释为什么mongoose在options字段保存一个数组数组以及如何解决这个问题。

2 个答案:

答案 0 :(得分:2)

更改此行:

"options": { "type": "Array", "ref": "SelectOptionItem" },

由此:

"options": { "type": "Mixed", "ref": "SelectOptionItem" },

或者通过这个:

"options": [SelectOptionItemSchema],

第二个为项目

生成_id

答案 1 :(得分:1)

有两种方法可以实现这一目标。

  • 首先,不为[42]
  • 创建单独的集合
  • 其次,创建新的sinkList集合并使用SelectOptionItem

使用mongoose SelectOptionItem。您可以尝试将此对象插入到数组中,而无需为ref创建新集合。如果您不需要,可以随时为4.13.1中的文档禁用SelectOptionItem属性。

_id

如果您想不惜任何代价为options创建集合,那么在创建文档时,您需要在数组中发送var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; var SelectOptionItemSchema= mongoose.Schema({ "name": { "type": "String", "required": true, "unique": true } }) let modelFields = { "name": { "type": "String", "required": true, "unique": true }, "description": { "type": "String" }, "options": [SelectOptionItemSchema], "company_id": { "type": "ObjectId", "ref": "Company", "required": true }, "deleted": { "type": "Boolean", "required": true }, "createdAt": { "type": "Number" }, "updatedAt": { "type": "Number" } }; var SelectOptionSchema = mongoose.Schema(modelFields) var SelectOption = mongoose.model('SelectOption', SelectOptionSchema); var data = { name: 'aa', description: 'aa', options: [ { name: '1' }, { name: '2' } ], company_id: '59b2cd9a072e4f28b839aaa0', deleted: false, createdAt: 1511569192524 } var newSelectOption = new SelectOption(data) 以获取选项。因为SelectOptionItem类似于外键。

您可以使用SelectOptionItem Ids在查询时获取完整的对象。有关更多查询技巧,请参阅http://mongoosejs.com/docs/populate.html

注意:我不确定这是多么准确,但您很可能希望将ref发送到.populate列。

  

注意:ObjectId,Number,String和Buffer可用作refs。   但是,除非您是高级用户,否则应使用ObjectId   有充分的理由这样做。

ObjectId