gpt4 book ai didi

MongoDB - 在 $elemMatch 中使用 $where

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

假设我有一个帐户集合,每个帐户都有一组用户:

{
_id: 1,
users: [
{
firstName: a,
lastName: b
},
{
firstName: c,
lastName: c
}
]
}

我想查询至少有一个用户具有相同 firstNamelastName 的所有帐户

我试着做这样的事情:

db.accounts.find({users: {
$elemMatch: {
$where: "this.firstName == this.lastName"
}
}})

但是当然因为这个错误它没有工作:

Can't canonicalize query: BadValue $elemMatch cannot contain $where expression

我想使用find 而不是aggregate。还有其他方法吗?

最佳答案

请尝试将 $whereArray.some 一起使用如下所示。

> db.collection.find({
$where: function() {
return this.users.some(function(obj){
return obj.firstName == obj.lastName;
})
}})

关于MongoDB - 在 $elemMatch 中使用 $where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36152070/

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