How do I search for an object by its ObjectId in the mongo console?

后端 未结 12 2623
心在旅途
心在旅途 2020-11-28 01:58

I\'ve found this question answered for C# and Perl, but not in the native interface. I thought this would work:

db.theColl.find( { _id: ObjectId(\"4ecbe7f9e8c

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 02:28

    I just had this issue and was doing exactly as was documented and it still was not working.

    Look at your error message and make sure you do not have any special characters copied in. I was getting the error

    SyntaxError: illegal character @(shell):1:43
    

    When I went to character 43 it was just the start of my object ID, after the open quotes, exactly as I pasted it in. I put my cursor there and hit backspace nothing appeared to happen when it should have removed the open quote. I hit backspace again and it removed the open quote, then I put the quote back in and executed the query and it worked, despite looking exactly the same.

    I was doing development in WebMatrix and copied the object id from the console. Whenever you copy from the console in WebMatrix you're likely to pick up some invisible characters that will cause errors.

提交回复
热议问题