mongodb - Create a field and assign objectId value for it in aggregation pipeline - Stack Overflow

This is an example of record stored in my mongo collection{"_id": "{"_id":

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
Add a comment  | 

1 Answer 1

Reset to default 2

First, 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745045128a4608049.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信