mongodb 按位检查不是 return 行
mongodb bitwise check not return row
我想按位检查我的数据,但不是 return data.Rights 默认参数是 1。我该如何解决这个问题?
db.getCollection('forms').find(
{
"IsActive" : true,
"$or" :
[
{ "$where" : "(this.Acls.Rights & 1) == 1" , "Acls.Actor._id" : ObjectId("5565f392a6df191eb4689bec") },
{ "$where" : "(this.Acls.Rights & 1) == 1" , "Acls.Actor._id" : ObjectId("5565f392a6df191eb4689bed") }
]
}
)
这是我希望匹配的文档:
{
"_id" : ObjectId("55686c44a6df1a1008c0b148"),
"IsActive" : true,
"Acls" : [
{
"_id" : ObjectId("557820b1a6df1a032c2c643a"),
"IsActive" : true,
"Actor" : {
"_id" : ObjectId("5565f392a6df191eb4689bec"),
"IsActive" : true,
"Name" : "admin",
"TypeId" : 2
},
"Department" : null,
"Rights" : NumberLong(1)
}
],
"AclCount" : 1
}
你这里的逻辑问题是 JavaScript 对 $where
clause does not have the same "abilities" as the MongoDB "native" search with regards to "dot notation" 的评估。
因此,您需要使用 JavaScript 方法为您要使用的属性评估 "each" 数组元素。为此,此处使用的最佳条件是内置 JavaScript .some()
数组。它 returns true/false
到条件 if "any" 的数组元素满足你的条件,最重要的是 "first" 是在没有遍历所有数组元素的情况下找到的
这也意味着(我讨厌在这里成为坏消息的承担者)如果不这样做,您在 "matching pair" 中比较 ObjectId
的逻辑在这里不起作用。
在正常情况下,您会使用 $elemMatch
来确保满足特定数组元素的两个属性,但您不能在 $elemMatch
中使用 $where
子句,所以所有逻辑需要在 JavaScript:
db.forms.find({
"IsActive": true,
"$or": [
{ "$where": "return this.Acls.some(function(el) { return ( el.Rights & 1 == 1 ) && el.Actor._id.equals(ObjectId("5565f392a6df191eb4689bec")) });"}
{ "$where": "return this.Acls.some(function(el) { return ( el.Rights & 1 == 1 ) && el.Actor._id.equals(ObjectId("5565f392a6df191eb4689bed")) });"}
]
})
鉴于 $where
的性能问题(不能为其条件使用索引),我强烈建议您在此处找到 "matching your ACL" 的替代方法,而不是您正在使用的 "bitwise" 方法.
是的,其他选项可能会带来更高的存储成本,但这里的权衡是您可以使用可以直接与 $elemMatch
子句匹配的元素获得更快的查询结果。
我想按位检查我的数据,但不是 return data.Rights 默认参数是 1。我该如何解决这个问题?
db.getCollection('forms').find(
{
"IsActive" : true,
"$or" :
[
{ "$where" : "(this.Acls.Rights & 1) == 1" , "Acls.Actor._id" : ObjectId("5565f392a6df191eb4689bec") },
{ "$where" : "(this.Acls.Rights & 1) == 1" , "Acls.Actor._id" : ObjectId("5565f392a6df191eb4689bed") }
]
}
)
这是我希望匹配的文档:
{
"_id" : ObjectId("55686c44a6df1a1008c0b148"),
"IsActive" : true,
"Acls" : [
{
"_id" : ObjectId("557820b1a6df1a032c2c643a"),
"IsActive" : true,
"Actor" : {
"_id" : ObjectId("5565f392a6df191eb4689bec"),
"IsActive" : true,
"Name" : "admin",
"TypeId" : 2
},
"Department" : null,
"Rights" : NumberLong(1)
}
],
"AclCount" : 1
}
你这里的逻辑问题是 JavaScript 对 $where
clause does not have the same "abilities" as the MongoDB "native" search with regards to "dot notation" 的评估。
因此,您需要使用 JavaScript 方法为您要使用的属性评估 "each" 数组元素。为此,此处使用的最佳条件是内置 JavaScript .some()
数组。它 returns true/false
到条件 if "any" 的数组元素满足你的条件,最重要的是 "first" 是在没有遍历所有数组元素的情况下找到的
这也意味着(我讨厌在这里成为坏消息的承担者)如果不这样做,您在 "matching pair" 中比较 ObjectId
的逻辑在这里不起作用。
在正常情况下,您会使用 $elemMatch
来确保满足特定数组元素的两个属性,但您不能在 $elemMatch
中使用 $where
子句,所以所有逻辑需要在 JavaScript:
db.forms.find({
"IsActive": true,
"$or": [
{ "$where": "return this.Acls.some(function(el) { return ( el.Rights & 1 == 1 ) && el.Actor._id.equals(ObjectId("5565f392a6df191eb4689bec")) });"}
{ "$where": "return this.Acls.some(function(el) { return ( el.Rights & 1 == 1 ) && el.Actor._id.equals(ObjectId("5565f392a6df191eb4689bed")) });"}
]
})
鉴于 $where
的性能问题(不能为其条件使用索引),我强烈建议您在此处找到 "matching your ACL" 的替代方法,而不是您正在使用的 "bitwise" 方法.
是的,其他选项可能会带来更高的存储成本,但这里的权衡是您可以使用可以直接与 $elemMatch
子句匹配的元素获得更快的查询结果。