gpt4 book ai didi

arrays - 查询对象与数组

转载 作者:可可西里 更新时间:2023-11-01 09:54:24 24 4
gpt4 key购买 nike

如何查询文档中的对象与对象数组?

我需要选择所有类似于数据集 2 的文档。

数据集 1:

{
'firstname' : 'John',
'lastname': 'Smith',
'assistance': [
{'type': 'Food', 'amount': 20}
]
}

数据集 2:

{
'firstname' : 'John',
'lastname': 'Smith',
'assistance': {
'type': 'Food',
'amount': 20
}
}

最佳答案

db.foo.find( { "assistance" : { $type : 3 } } ); // 3 is bson type for object

将同时返回文件和

db.foo.find( { "assistance" : { $type : 4 } } ); // 4 is bson type for object

不会返回两个文档中的任何一个。

这是因为在 mongodb 中,当查询数组时,会检查数组元素而不是整个数组。

您可以通过以下方式消除辅助类型为数组的所有元素:

db.foo.find({"assistance.0" : {$exists : false}})

关于arrays - 查询对象与数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41523802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com