如果键并不总是存在,如何在mongodb中找到_one?

发布于 2025-02-10 17:58:34 字数 762 浏览 1 评论 0原文

我有一个以下mongodb:

某些记录看起来像{'_ ID':ObjectID('62790CE20375A639AF1D8676'),'visit_id':594817704,'trasmaction':10

} {'_id':ObjectId('62790CE20375A639AF1D8679'),'visit_id':594817704}

使用此代码我可以搜索由TransAction_ID组成的所有集合:

collection.find({'transaction_id': { "$exists":True }

但是,如果我想获得具有特定transaction_id的记录,代码不起作用(这花费了无限的时间搜索):

collection.find({'transaction_id': 10})

我尝试结合这两种方法,也就是说,首先询问Transaction_ID是否存在,然后搜索TransAction_ID = 10,但它不起作用:

collection.find({'transaction_id': { "$exists":True }, 'transaction_id': 10})

我该如何修复它?

I have a following mongodb:

some records looks like {'_id': ObjectId('62790ce20375a639af1d8676'), 'visit_id': 594817704, 'transaction' : 10}

some has only this form: {'_id': ObjectId('62790ce20375a639af1d8679'), 'visit_id': 594817704}

Using this code I can search for all collections that consists of transaction_id:

collection.find({'transaction_id': { "$exists":True }

But if I would like to get a record with specific transaction_id, this code does not work (it tooks infinite time to search):

collection.find({'transaction_id': 10})

I tried to combine both approaches, that is to firstly ask if transaction_id exists and then search for transaction_id = 10, but it did not work:

collection.find({'transaction_id': { "$exists":True }, 'transaction_id': 10})

How can I fix it, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2025-02-17 17:58:34
collection.findOne({transaction_id: 10})

应该工作。

如果您想要特定的值,则不需要$ $。

请查看其在 Playground示例示例

您的数据集有多大的方面?您在transaction_id上有索引吗?

如果查询速度很慢,请考虑添加transaction_id上的哈希索引。
如果没有索引,数据库应检查所有文档,以查看它们是否具有transaction_id:1000。这是o(n)操作,其中n是您的文档数量。该索引存储此信息,因此仅检查o(1)

为了创建索引,请使用UI或使用shell:

db.getCollection(<collectionName>).createIndex( {transaction_id: "hashed" })

在其中您将&lt; collectionName&gt;用收集名称替换。

collection.findOne({transaction_id: 10})

should work.

No need for $exists if you want a specific value.

See how it works on the playground example

How big is your data set? do you have an index on transaction_id?

If your query is slow, consider add an hashed index on transaction_id.
Without the index, the DB should check all documents to see if any of them has transaction_id: 1000. This is O(n) operations where n is the number of your documents. The index stores this information, so it will be one check only O(1).

In order to create the index use the UI or, using the shell:

db.getCollection(<collectionName>).createIndex( {transaction_id: "hashed" })

Where you replace <collectionName> with your collection name.

攀登最高峰 2025-02-17 17:58:34

请参考我的代码。

from pymongo import MongoClient

client = MongoClient('localhost', 27017);

#connect to the DB named testdb
mydatabase = client.testdb

#pickup collection named transactions 
collection = mydatabase.transactions

#find one by transaction_id
res = collection.find_one({transaction_id:10})

#display data
print(res)

创建索引

collection.create_index("transaction_id");

Please refer my code.

from pymongo import MongoClient

client = MongoClient('localhost', 27017);

#connect to the DB named testdb
mydatabase = client.testdb

#pickup collection named transactions 
collection = mydatabase.transactions

#find one by transaction_id
res = collection.find_one({transaction_id:10})

#display data
print(res)

To create index

collection.create_index("transaction_id");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文