简体   繁体   English

在mongodb中无法通过ID查找

[英]cannot find by id in mongodb

I have problem with query in MongoDB. 我在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#). _id是从GUID(C#)生成的。 And the problem is when i want do native query in mongo console. 问题是当我想在mongo控制台中进行本机查询时。

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. 目前,Mongo对GUID的支持非常有限。 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. C#驱动程序当前正在以二进制表示形式编写GUID,其中GUID的前三个字段是little endian,而其他一些驱动程序则使用big endian表示。 So depending on which driver stored the GUID the string representation would be different, see this JIRA ticket for further detail, 因此,根据存储GUID的驱动程序的不同,字符串表示形式会有所不同,有关更多详细信息,请参见JIRA票证,

In short, as your GUID is created by C#, it is stored as a BinData object with type 3 as shown below. 简而言之,由于GUID由C#创建,因此将其存储为类型3的BinData对象,如下所示。

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时从GitHub加载帮助程序脚本:

mongo --shell uuidhelpers.js

When this is loaded you can query for your GUID like this: 加载后,您可以像这样查询GUID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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