- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个看起来像这样的 MongoDB 集合:
{
players: [
{uuid: "A"},
{uuid: "B"}
]
},
{
players: [
{uuid: "A"},
{uuid: "C"}
]
},
{
players: [
{uuid: "D"},
{uuid: "E"}
]
}
$lookup
查找玩家出现的所有文档。阶段:
from: "collection",
pipeline: [
{
$match: {
players: {
$elemMatch: {
uuid: "$playerId"
}
}
//using "players.uuid": "$playerId" doesn't work either
}
}
],
as: "field"
$lookup
的输入舞台看起来像这样:
{
"playerId" : "A"
}
{
"playerId" : "B"
}
{
"playerId" : "C"
}
field
中返回一个空数组.好像
$uuid
没有得到正确评估,因为如果我交换
$uuid
使用硬编码值(例如
A
),此查询返回结果。
let
属性(property),这给了我同样的结果。
最佳答案
使用您提供的文件。我相信这可能对你有用:
我用过 $lookup
加入包含 playerId
的集合,这会创建一个 array
叫 field
.然后我使用 $unwind
从 field
中提取所有数组元素和 player
.最后我使用 $cond
交叉检查两个值是否匹配。
db.getCollection('foo').aggregate([
{ $lookup : {
from: "bar",
localField: "players.uuid",
foreignField: "playerId",
as: "field"
} },
{ $unwind : "$players" },
{ $unwind : "$field" },
{ $project : {
"players": 1,
"field" : 1,
"isMatch": {
"$cond": [ { "$eq": ["$players.uuid", "$field.playerId"] }, 1, 0 ]
} } }
])
/* 1 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 1.0
}
/* 2 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f539b337e8d2b97ff2fff"),
"playerId" : "B"
},
"isMatch" : 0.0
}
/* 3 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "B"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 0.0
}
/* 4 */
{
"_id" : ObjectId("5a7f534b337e8d2b97ff2ffb"),
"players" : {
"uuid" : "B"
},
"field" : {
"_id" : ObjectId("5a7f539b337e8d2b97ff2fff"),
"playerId" : "B"
},
"isMatch" : 1.0
}
/* 5 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 1.0
}
/* 6 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "A"
},
"field" : {
"_id" : ObjectId("5a7f53a8337e8d2b97ff3000"),
"playerId" : "C"
},
"isMatch" : 0.0
}
/* 7 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "C"
},
"field" : {
"_id" : ObjectId("5a7f5374337e8d2b97ff2ffe"),
"playerId" : "A"
},
"isMatch" : 0.0
}
/* 8 */
{
"_id" : ObjectId("5a7f5356337e8d2b97ff2ffc"),
"players" : {
"uuid" : "C"
},
"field" : {
"_id" : ObjectId("5a7f53a8337e8d2b97ff3000"),
"playerId" : "C"
},
"isMatch" : 1.0
}
关于$lookup 阶段内的 MongoDB 聚合 $elemMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48721925/
根据 mongodb 文档,$elemMatch 的语法是, t.find( { x : { $elemMatch : { a : 1, b : { $gt : 1 } } } } ) 我试过了,效果
我有以下文件 { "_id" : ObjectId("52da43cd6f0a61e8a5059aaf"), "assignments" : [ {
我正在为我的应用中的项目(产品)进行用户评分(赞/不赞)。 我似乎能够通过对象数组实现我想要的,但我想知道 mongodb 是否提供了一种方法来使用数字数组 (user_ids) 实现这一点。 db.
我有以下文件 { "_id" : ObjectId("52da43cd6f0a61e8a5059aaf"), "assignments" : [ {
我正在为我的应用中的项目(产品)进行用户评分(赞/不赞)。 我似乎能够通过对象数组实现我想要的,但我想知道 mongodb 是否提供了一种方法来使用数字数组 (user_ids) 实现这一点。 db.
这是我的对象: { "_id" : ObjectId("53fdcb6796cb9b9aa86f05b9"), "list" : [ "a", "b" ], "complist" : [ { "a"
我正在尝试查找与条件匹配的数组的最后一个元素,例如,如果我有来自 $elemMatch 的数据页: { _id: 1, students: [ { name: "john", scho
我有以下数据结构 { "_id" : ObjectId("523331359245b5a07b903ccc"), "a" : "a", "b" : [ {
我有一个 unitScores 集合,其中每个文档都有一个 id 和一个文档数组,如下所示: "_id": ObjectId("52134edd5b1c2bb503000001"), "scores"
我的 MongoDB 文档结构如下: {_id: ObjectId("53d760721423030c7e14266f"), fruit: 'apple', vitamins: [ {
考虑以下文档: { "_id" : "ID_01", "code" : ["001", "002", "003"], "Others" : "544554" } 我经历了这个MongoDB
所以,我有一个数据库,其中包含大量文档中的数组。我想使用 $in 找到我的查询与一个或多个数组元素完全匹配的整个文档。 所以,文档结构: { "_id": "76561198045636214",
查询运算符$and的用法有什么逻辑区别吗? db.collection.find({$and: [{"array.field1": "someValue"}, {"array.field2": 3}]
我知道您可以使用 $elemMatch 作为投影来限制子集合数组中的项目。当这样使用它时,它会返回匹配的子文档的所有字段,无论是否也指定了 query projections。 是否可以限制匹配子文档
我有一个像这样的集合(摘要)。 { "id":"summaryid", "locations": [ { "id": "loc1",
我有一个包含以下形式文档的集合: { "_id" : { "$oid" : "67bg............"}, "ID" : "xxxxxxxx", "senses"
我有一个像这样的集合(摘要)。 { "id":"summaryid", "locations": [ { "id": "loc1",
假设我有这样一个文档 { title : 'myTitle', favorites : [{name : 'text', number : 6}, {name : 'other', numbe
我的查询: { 'objects.item.opts1.opts2': { '$elemMatch': [ { name: 'false' } ] } } 返回:数组 opts2 中包含任何内容的任何
如何使用 elemMatch 对 SubDocument 数组进行搜索?我有一个名为 ReportCollection 的文档,其中包含以下元素:- /* 0 */ { "_id" : Obj
我是一名优秀的程序员,十分优秀!