0
db.getCollection('User').find({
    "userId" : ObjectId("5a141ac4048378xb52c3e5a9"),
        "userRole" : "ADMIN",
        "Id" : "1234567890"})

    result:
    {
        "userId" : ObjectId("5a141ac4048378xb52c3e5a9"),
        "userRole" : "ADMIN",
        "Id" : "1234567890"
    }

    Expecting output:
    {
        "userId" : "5a141ac4048378xb52c3e5a9",
        "userRole" : "ADMIN",
        "Id" : "1234567890"
    }

I am very new to mongodb i nedded to return objectId as String ,i neeed some suggestion to do that.

2 Answers 2

2

It can be done simply using the below approach

db.User.find({"userId": objectId("5a141ac4048378ab52c3e5a9")}).map(
      function(doc) { 
         return { "userId": doc.userId.str}
      });

Please see ObjectId for more methods

Sign up to request clarification or add additional context in comments.

1 Comment

myDocument._id.str
0

You can try use a aggregation But ObjectId isn't strings, it's just numbers, why you want present it as string?

1 Comment

Aggregation is not needed here, it can be done using simple find query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.