简体   繁体   English

如何使用$ not和$ elemMatch使用羽毛猫鼬查询更新

[英]How to use feathers mongoose query update with $not and $elemMatch

I'm trying updated my document using feathersjs method update by server side like as shown below: 我正在尝试通过服务器端使用feathersjs方法更新来更新我的文档,如下所示:

context.app.service('myDocumens').update({
  _id: obj._id,
  'data': {
    "$not": {
      "$elemMatch": {
        'year': 2018
      }
    }
  }
}, {
  $addToSet: {
    'data': {
      'position': 'senior',
      'group': 1,
      'comment': "",
      'year': 2018
    }
  }
}).then(result => {    
  console.log(result)    
});}

I show below sample document: 我在下面的示例文档中显示:

{
  "_id": ObjectId("...."),
  "firstName": "Adam",
  "lastName": "Brown",
  "data": [
     { "position":"senior","group":1,"comment":"","year":2018 },
     { "position":"junior","group":0,"comment":"","year":2017 }
  ]
}

Using above query I try add to the array new object witch contain a field 'year': 2018 if it not exists in this array but all the time after run this query to array is added new object although the array contains already an object with field 'year':2018. 使用上面的查询,我尝试将新对象添加到数组中,并包含一个字段“ year”:2018(如果此数组中不存在该字段,但是在对数组运行此查询后的所有时间都添加新对象,尽管该数组已经包含一个带有字段的对象'年':2018。 What is wrong? 怎么了?

When I run above query in mongoDb shell, it works fine. 当我在mongoDb shell中运行以上查询时,它工作正常。

The main thing I notice is that you're using the feathersjs adapter.update API incorrectly. 我注意到的主要问题是您错误地使用了feathersjs adapter.update API。 The reference to your document ID should be outside of your object. 对文档ID的引用应在对象之外。 Try switching to this signature: 尝试切换到此签名:

context.app.service('myDocumens').update(id, data, params);

See the API documentation for more information. 有关更多信息,请参见API文档。 https://docs.feathersjs.com/api/databases/common.html#adapterupdateid-data-params https://docs.feathersjs.com/api/databases/common.html#adapterupdateid-data-params

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM