【问题标题】:how to save document in table without _id [duplicate]如何在没有_id的表中保存文档[重复]
【发布时间】:2017-12-04 14:00:57
【问题描述】:

我不会在没有 _id 的情况下保存对象员工我只想保存属性“employe”如下:

"employe" :[
              {
                    "name" : "leila", 
                    "idemploye" : ObjectId("59319505efa50b137477a1f4"), 
              },
              {
                    "name" : "aicha", 
                    "idemploye" : ObjectId("59319505efa50b137477a1f"),     
              }
            ]

这个和用自动生成的_id属性记录在db上的文档

    { 
            "_id" : ObjectId("59563cb90f62370da8b598fc"), 
            "categorie" : "MAQUILLAGE --> Maquillage Jour", 
            "idsalon" : ObjectId("59318cb0efa50b137477a149"), 
            "idservice" : ObjectId("5931828defa50b137477a137"), 
            "updatedAt" : ISODate("2017-06-30T11:57:45.897+0000"), 
            "createdAt" : ISODate("2017-06-30T11:57:45.897+0000"), 
            "isChecked" : true, 
            "employe" : [
                {
                    "name" : "leila", 
                    "idemploye" : ObjectId("59319505efa50b137477a1f4"), 
                    "_id" : ObjectId("59563cb90f62370da8b598fd")
                },
 {
                    "name" : "aicha", 
                    "idemploye" : ObjectId("59319505efa50b137477a1f"), 
                    "_id" : ObjectId("59563cb90f62370da8b598fb")
                }
            ], 
            "price" : NumberInt(15), 
            "time" : NumberInt(5), 
            "name" : "Forfait Cils et Sourcils", 
            "__v" : NumberInt(0)
        }

我的属性员工模式是:

 employe: [{
        name :String,
        idemploye: {
            type: mongoose.Schema.Types.ObjectId,
            ref: '_id'
        }
    }]
    ,

【问题讨论】:

    标签: javascript node.js mongoose


    【解决方案1】:

    唯一的方法是为您的员工属性创建子架构,如下所示:

    var employeSchema = mongoose.Schema({
      name: String,
      idemploye: {
        type: mongoose.Schema.Types.ObjectId,
        ref: '_id'
      }
    }, {
      _id: false
    });
    
    var parentSchema = mongoose.Schema({
      ...
      employe: [employeSchema]
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 2022-11-04
      相关资源
      最近更新 更多