如何为 MongoDB 中的数组提取偶数?
How to pull even numbers for an Array in MongoDB?
假设我们有以下文档:
db.test.insert({a: [2, 3, 4, 5, 6, 7, 8]})
如何去掉所有的偶数只剩下[3, 5, 7]
?这可以通过使用 $pull
运算符来完成吗?
您可以使用 $pull
update operator and the $mod
query operator. To update multiple documents I suggest you use the updateMany
method and for single update you should use the updateOne
method because update
从数组中提取所有偶数元素,在官方语言驱动程序中突出显示已弃用。问题是 shell 方法落后于驱动程序。也没有人真正在 shell.
中编写应用程序
db.test.updateMany({}, { "$pull": { "a": { "$mod": [ 2, 0 ] } } } )
假设我们有以下文档:
db.test.insert({a: [2, 3, 4, 5, 6, 7, 8]})
如何去掉所有的偶数只剩下[3, 5, 7]
?这可以通过使用 $pull
运算符来完成吗?
您可以使用 $pull
update operator and the $mod
query operator. To update multiple documents I suggest you use the updateMany
method and for single update you should use the updateOne
method because update
从数组中提取所有偶数元素,在官方语言驱动程序中突出显示已弃用。问题是 shell 方法落后于驱动程序。也没有人真正在 shell.
db.test.updateMany({}, { "$pull": { "a": { "$mod": [ 2, 0 ] } } } )