简体   繁体   中英

cannot find by id in mongodb

I have problem with query in MongoDB.

I have document with that structure

{
    "_id" : LUUID("5eca9329-6525-e544-bb27-f1797def8110"),
    "StartTimestamp" : NumberLong(193),
    "EndTimestamp" : NumberLong(193),
}

_id is generate from GUID (C#). And the problem is when i want do native query in mongo console.

My query

db.getCollection('Object').findOne(
{
    "_id": LUUID("5eca9329-6525-e544-bb27-f1797def8110")
})

And then i have non result

Mongo's support for GUID is limited at the moment. C# drivers are currently writing GUIDs in a binary representation where the first three fields of the GUID are little endian , whereas some other drivers are using the big endian representation. So depending on which driver stored the GUID the string representation would be different, see this JIRA ticket for further detail,

In short, as your GUID is created by C#, it is stored as a BinData object with type 3 as shown below.

BinData(3,"KZPKXiVlROW7J/F5fe+BEA==")

This means that in order to retrieve your record, you will have to run the following query:

db.getCollection('Object').findOne(
{
   "_id": BinData(3,"KZPKXiVlROW7J/F5fe+BEA==")
})

Alternatively you can load the helper script from GitHub when starting a Mongo shell:

mongo --shell uuidhelpers.js

When this is loaded you can query for your GUID like this:

db.data.find({_id:CSUUID("5eca9329-6525-e544-bb27-f1797def8110")})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM