admin管理员组文章数量:1410705
This is an example of record stored in my mongo collection
{
"_id": "{\"_id\": {\"$oid\": \"67a20f0adee77ef71adfe8bc\"}, \"copyingData\": true}",
"operationType": "insert",
"fullDocumentBeforeChange": null,
"fullDocument": "{\"_id\": {\"$oid\": \"67a20f0adee77ef71adfe8bc\"}, \"name\": \"Nazar\", \"adress\": \"Kyiv\", \"date\": \"2024-02-05T10:00:00Z\"}",
"ns": {
"db": "users",
"coll": "users"
},
"to": null,
"documentKey": "{\"_id\": {\"$oid\": \"67a20f0adee77ef71adfe8bc\"}}",
"updateDescription": null,
"clusterTime": null,
"txnNumber": null,
"lsid": null
}
I need create identifier
field and assign the value of documentKey._id.$oid
value to it.
As you can see, documentKey
is not an object, but a string, so I tried to convert it to object first, hoping that I could take $oid directly from that object, but it was not fully parsed as object
The aggregation that I tried
db.events.aggregate([
{
$set: {
documentKey: {
$cond: {
if: { $eq: [{ $type: "$documentKey" }, "string"] },
then: {
$function: {
body: "function(str) { return JSON.parse(str); }",
args: ["$documentKey"],
lang: "js"
}
},
else: "$documentKey"
}
}
}
},
{
$set: {
"identifier": "$documentKey._id.$oid"
}
}
]);
MongoServerError[Location16410]: Invalid $set :: caused by :: FieldPath field names may not start with '$'. Consider using $getField or $setField.
This is an example of record stored in my mongo collection
{
"_id": "{\"_id\": {\"$oid\": \"67a20f0adee77ef71adfe8bc\"}, \"copyingData\": true}",
"operationType": "insert",
"fullDocumentBeforeChange": null,
"fullDocument": "{\"_id\": {\"$oid\": \"67a20f0adee77ef71adfe8bc\"}, \"name\": \"Nazar\", \"adress\": \"Kyiv\", \"date\": \"2024-02-05T10:00:00Z\"}",
"ns": {
"db": "users",
"coll": "users"
},
"to": null,
"documentKey": "{\"_id\": {\"$oid\": \"67a20f0adee77ef71adfe8bc\"}}",
"updateDescription": null,
"clusterTime": null,
"txnNumber": null,
"lsid": null
}
I need create identifier
field and assign the value of documentKey._id.$oid
value to it.
As you can see, documentKey
is not an object, but a string, so I tried to convert it to object first, hoping that I could take $oid directly from that object, but it was not fully parsed as object
The aggregation that I tried
db.events.aggregate([
{
$set: {
documentKey: {
$cond: {
if: { $eq: [{ $type: "$documentKey" }, "string"] },
then: {
$function: {
body: "function(str) { return JSON.parse(str); }",
args: ["$documentKey"],
lang: "js"
}
},
else: "$documentKey"
}
}
}
},
{
$set: {
"identifier": "$documentKey._id.$oid"
}
}
]);
MongoServerError[Location16410]: Invalid $set :: caused by :: FieldPath field names may not start with '$'. Consider using $getField or $setField.
Share
edited Mar 4 at 11:53
Filburt
18.1k13 gold badges90 silver badges149 bronze badges
asked Mar 4 at 11:52
Nazar MazurykNazar Mazuryk
12 bronze badges
1 Answer
Reset to default 2First, get the hex-string 67a20f0adee77ef71adfe8bc
(for example) from $oid
using $getField
with $literal
.
Entries like { "$oid": "67a20f0adee77ef71adfe8bc" }
are the Extended JSON notation for the BSON ObjectID type. So, to make that an actual ObjectID, use $toObjectId
.
So your second $set
stage would be:
{
$set: {
identifier: {
$toObjectId: {
$getField: {
field: { $literal: "$oid" },
input: "$documentKey._id"
}
}
}
}
}
Mongo Playground
本文标签: mongodbCreate a field and assign objectId value for it in aggregation pipelineStack Overflow
版权声明:本文标题:mongodb - Create a field and assign objectId value for it in aggregation pipeline - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745045128a2639315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论