0

When I post data to the MongoDB database via node.js, id is generated like below

  {
            "_id": "619908d9b5ac0448fe368b35",
            "firstname": "ccccc",
            "createdAt": "2021-11-20T14:40:25.561Z",
            "updatedAt": "2021-11-20T14:40:25.561Z",
            "__v": 0
        }

How can I change it like this

{
        "id": "1,
        "firstname": "ccccc",
        "createdAt": "2021-11-20T14:40:25.561Z",
        "updatedAt": "2021-11-20T14:40:25.561Z",
        "__v": 0
    }
  • 1
    Every MongoDB record must have a field `_id`. You can have field `id` too, but every record must have a field `_id`. The field `_id` is special - it must be unique, and it is the only immutable field in MongoDB. It can hold any data type except arrays. I recommend you use it rather than adding another similar field called `id`. – barrypicker Nov 23 '21 at 18:34
  • 1
    This field `_id` is considered the primary key to the collection and will always have an index. – barrypicker Nov 23 '21 at 18:35
  • I think this is what you're looking for: https://stackoverflow.com/questions/7034848/mongodb-output-id-instead-of-id – tonilaukka Apr 17 '22 at 08:27

0 Answers0