英文:
MongoDB: Aggregation to convert ObjectId to DBRef
问题
以下是翻译好的部分:
"Is it possible to have an aggregation that converts an objectId column into a dbRef? I am attempting to use aggregation and $merge to transform 1 table with an ObjectId column into a DBRef one
{
$addFields: {
name: "$field",
parentId: {
$cond: {
if: { $ne: ["$parentId", null] },
then: DBRef("parents", "$parentId"),
else: null
}
}
},
},
tried this but got
> An object representing an expression must have exactly one field"
英文:
Isit possible to have an aggregation that converts an objectId column into a dbRef? I am attempting to use aggregation and $merge to transform 1 table with an ObjectId column into a DBRef one
{
$addFields: {
name: "$field",
parentId: {
$cond: {
if: { $ne: ["$parentId", null] },
then: DBRef("parents", "$parentId"),
else: null
}
}
},
},
tried this but got
> An object representing an expression must have exactly one field
答案1
得分: 1
I think it is. Internally DBRefs
are stored in this structure in MongoDB,
{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }
So when you execute your query, the parentId
becomes of this form, and you get an error because MongoDB interprets any key beginning with $
to be an operator in the aggregation pipeline. You can overcome this using, $setField
like this:
db.collection.aggregate([
{
"$addFields": {
"parentIdCopy": {
$cond: {
if: {
$ne: [
"$parentId",
null
]
},
then: {
"$setField": {
"field": {
$literal: "$ref"
},
"input": {},
"value": "parents"
},
},
else: null
}
}
}
},
{
"$addFields": {
"parentIdCopy": {
$cond: {
if: {
$ne: [
"$parentId",
null
]
},
then: {
"$setField": {
"field": {
$literal: "$id"
},
"input": "$parentIdCopy",
"value": "$parentId"
},
},
else: null
}
}
}
},
{
"$set": {
"parentId": "$parentIdCopy",
parentIdCopy: "$$REMOVE"
}
},
{
"$merge": {
"into": "collection",
"on": "_id",
"whenMatched": "replace",
}
}
])
In this query, we are manually generating the object in DBRef
format, rather than using the constructor. Playground link.
英文:
I think it is. Internally DBRefs
are stored in this structure in MongoDB,
{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }
So when you execute your query, the parentId
becomes of this form, and you get an error because MongoDB interprets any key beginning with $
to be an operator in the aggregation pipeline. You can overcome this using, $setField
like this:
db.collection.aggregate([
{
"$addFields": {
"parentIdCopy": {
$cond: {
if: {
$ne: [
"$parentId",
null
]
},
then: {
"$setField": {
"field": {
$literal: "$ref"
},
"input": {},
"value": "parents"
},
},
else: null
}
}
}
},
{
"$addFields": {
"parentIdCopy": {
$cond: {
if: {
$ne: [
"$parentId",
null
]
},
then: {
"$setField": {
"field": {
$literal: "$id"
},
"input": "$parentIdCopy",
"value": "$parentId"
},
},
else: null
}
}
}
},
{
"$set": {
"parentId": "$parentIdCopy",
parentIdCopy: "$$REMOVE"
}
},
{
"$merge": {
"into": "collection",
"on": "_id",
"whenMatched": "replace",
}
}
])
In this query, we are manually generating the object in DBRef
format, rather than using the constructor. Playground link.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论