在mongoDB中删除数组字段大小小于3的文档 [英] remove documents with array field's size less than 3 in mongoDB

查看:207
本文介绍了在mongoDB中删除数组字段大小小于3的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为col的mongoDB集合,其中包含如下所示的文档

i have a mongoDB collection named col that has documents that look like this

{
  {
    intField:123,
    strField:'hi',
    arrField:[1,2,3]
  },

  {
    intField:12,
    strField:'hello',
    arrField:[1,2,3,4]
  },

  {
    intField:125,
    strField:'hell',
    arrField:[1]
  }
}

现在,我想从数组字段size小于2的集合col中删除文档.

Now i want to remove documents from collection col in which size of the array field is less than 2.

所以我写了一个看起来像这样的查询

So i wrote a query that looks like this

db.col.remove({'arrField':{"$size":{"$lt":2}}})

现在此查询不执行任何操作.我用db.col.find()检查过,它返回了所有文档.该查询出了什么问题?

Now this query doesnt do anything. i checked with db.col.find() and it returns all the documents. Whats wrong with this query?

推荐答案

摘自

您不能使用$ size查找一定范围的大小(例如:元素多于1个的数组).

You cannot use $size to find a range of sizes (for example: arrays with more than 1 element).

文档建议您维护一个单独的size字段(在这种情况下为arrFieldSize),如果要尝试这种事情,请使用数组中的项目计数.

The docs recommend maintaining a separate size field (so in this case, arrFieldSize) with the count of the items in the array if you want to try this sort of thing.

这篇关于在mongoDB中删除数组字段大小小于3的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆