【问题标题】:$push in MongoDb not working?$push 在 MongoDb 中不起作用?
【发布时间】:2016-02-26 13:43:22
【问题描述】:

我的架构如下所示:

    var exampleSchema = newSchema({
    profile:{
    experience :[{
              exp : String
    }]
   }
  }); 

这是更新个人资料收集经验的代码:

exampleSchema.statics.experience = function (id,experience, callback){
var update = {
        $push: {
          'profile.experience': experience
        }
      }
      this.findByIdAndUpdate(id,update,function(err) {
        if (err) {
          callback(err);
        } else {
          callback(null);
        }
      })

我收到了类似The field 'profile.experience' must be an array but is of type String in document {_id: ObjectId('5653f1d852cf7b4c0bfeb54a')}[object Object]的错误

console.log(experience) 等于

{ exp: 'jlkjlkjlk' }

我的收藏应该是这样的:

experience:[
   {
    exp : "YYYY"
    },
   {
    exp:"xxxx"}
 ]

【问题讨论】:

  • 您正在尝试更新不是数组的字段“profile.experience.**exp**”。您必须将 $push 应用于“profile.experience”。
  • 很抱歉我遇到了同样的错误
  • 您的架构定义不明确。经验定义为experience :[{ exp : String ]},应为experience :[{ exp : String **}]**
  • 谢谢,但在我的示例中,它的定义是正确的......
  • 你能确定所有插入'profile.experience'的记录都是数组吗?您似乎在文档中有一个字符串 {_id: ObjectId('5653f1d852cf7b4c0bfeb54a')}

标签: arrays node.js mongodb mongoose


【解决方案1】:

您可以使用$set 而不是$push,这可能会起作用。

$set: {
    'profile.experience': experience
}

【讨论】:

  • 是的,它只有在我们已经有经验数组的情况下才会起作用,如果同时发生更新经验会发生什么,那么它将有无效的数据......它是最好分开执行推拉...
【解决方案2】:

首先,您必须检查您将字段声明为这样的数组(查看字段产品):

   shop = {
        'name': "Apple Store",
        'description': "",
        'direction': "",
        'contact': "",
        'products':[]
    }

现在,如果您想使用 $push 向字段产品添加一些内容

product = {
        'name': "Iphone 6",
        'description': "Iphone model 6, 64GB",
        'price': 700,
        'count': 3 
    }
 myquery = { "name" : "Apple Store" }
 obj ={"$push":{"products":{"$each": [product]}}}
 db.collection.update_one(myquery,obj)

此代码是为 PyMongo 框架提供的。要在 MongoDB 中使用,直接用 update 替换 update_one。 Mongo resource

【讨论】:

    【解决方案3】:

    您是否正在搜索将多个值添加到单个字段中,然后使用这个。 把这个写成你的模型或架构:

        arrayremarks:[{remark: String}]
    

    然后在你的控制器中写入:

    module.exports.addingremarks = (req, res) => {
        let casenum=JSON.parse(JSON.stringify(req.body.casenum).replace(/"\s+|\s+"/g,'"'))
        var rem={remark:"Suman macha"}
        Inwart.update( { 'casenum': casenum },{ $push: { arrayremarks:rem} } ,function (err, inwarts) {
            if (err)
             return console.error(err); 
             res.send(inwarts);  
         }
      )
    }
    

    【讨论】:

      【解决方案4】:

      我改变了这样的架构

       experience          : [{type:String,exp:String}],
      

      我的更新对象是这样的

       var update = {
          $push: {
            'profile.experience': san.exp
      }
      };
      

      san 看起来像这样:{ exp: 'YYY' }

      在 mongoose 集合里面看起来像这样使用 RoboMongo

       "experience" : [ 
              "experienced in XXX", 
              "YYY"
          ],
      

      【讨论】:

        【解决方案5】:

        想象一下你有这个收藏:

        /* 1 */
        {
            "_id" : ObjectId("565425e862760dfe14339ba8"),
            "profile" : {
                "experience" : [ 
                    {
                        "exp" : "Experto"
                    }
                ]
            }
        }
        
        /* 2 */
        {
            "_id" : ObjectId("565425f562760dfe14339ba9"),
            "profile" : {
                "experience" : {
                    "exp" : "Experto"
                }
            }
        }
        
        /* 3 */
        {
            "_id" : ObjectId("5654260662760dfe14339baa"),
            "profile" : {
                "experience" : "Experto"
            }
        }
        

        如果你尝试(更新文档 /* 2 */):

        db.profile.update(
           { _id: ObjectId("565425f562760dfe14339ba9") },
           { $push: { "profile.experience" : { exp : "Intermediate" } } }
        )
        

        你得到这个错误:

        字段“profile.experience”必须是数组,但类型为 Object 在文档中 {_id: ObjectId('565425f562760dfe14339ba9')}

        如果你尝试(更新文档 /* 3 */):

        db.profile.update(
           { _id: ObjectId("5654260662760dfe14339baa") },
           { $push: { "profile.experience" : { exp : "Intermediate" } } }
        )
        

        你会得到:

        字段“profile.experience”必须是数组,但类型为字符串 在文档中 {_id: ObjectId('5654260662760dfe14339baa')}

        【讨论】:

        • 解决方案?
        【解决方案6】:
        $push: {
            'profile.experience': experience
        }
        

        删除.exp

        【讨论】:

        • 我收到此错误:“profile.experience”字段必须是一个数组,但在文档中属于字符串类型
        猜你喜欢
        • 2018-10-14
        • 2018-04-27
        • 2017-06-30
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        • 2013-05-10
        • 2018-02-08
        • 2017-01-16
        相关资源
        最近更新 更多