【发布时间】:2019-03-11 02:23:10
【问题描述】:
我必须将父文档的嵌入字段与最后一个文档的嵌入字段(存在于数组中)进行比较,后者也是父文档的一部分,并在这两个字段不匹配时返回文档的externalIds。
以下图片供参考:
[包含数组的实际元数据][1]
[这是包含该字段的另一张图片][2]
所以在第二张图片中,我实际上只想要最后一个修订,即修订数组中的最后一个文档与父文档进行比较,需要比较的字段是metadata.formData.mergeStatus.status。
我想要那些文档_ids,其中数组中的最后一个文档的字段与其父文档的相应字段不同,如果存在修订数组的话。
我是 mongodb 的新手,并且是第一次在 stackoverflow 中询问,所以如果你觉得它很愚蠢,请原谅,但是这是我的尝试。
db.document.aggregate([
{ $match: { $and: [
{ spaceId: 3007 },
{ revisions: { $exists: true, $not: { $size: 0 } } }
] } },
{ $project:
{ _id: 1,
spaceId:1,
externalId:1,
revisions: { $cond: {
if: { $isArray: "$revisions" },
then: { $size: "$revisions" },
else: "NA" } },
last: { $arrayElemAt: [ "$revisions", -1 ] },
difference: { $ne: [
"last.metadata.formData.mergeStatus.status",
"metadata.formData.mergeStatus.status" ] }
}
}
]);
The above suggestion really helps however i am confused for below scenario,
so the metadata field contains elementsData and inside of that is another embedded object signature, which has two string fields
1.userSignatureDate(String)
2.userSignatureText(String)
so when i have applied above logic to compare the *userSignatureDate* of parent metadata with respective field in last document of the revisions array.it is not giving actual results.
here is the image for the code that i had written and the sample metadata Structure .[enter image description here][3][enter image description here][4]
[1]: https://i.stack.imgur.com/rsrKv.png
[2]: https://i.stack.imgur.com/y6wsp.png
[3]: https://i.stack.imgur.com/398I8.png
[4]: https://i.stack.imgur.com/PYUAL.png
【问题讨论】:
标签: mongodb mongodb-query aggregation-framework