如何比较两个集合中的两个值 - pymongo?
我有两个收藏。 例如;
collection1:
[
{
_id: ObjectId("6253492b634e9f3970c5dc86"),
ID: 90448,
Name: 'Hostname1',
Identifier: 'hostname_example1'
},
{
_id: ObjectId("6253492b634e9f3970c5dc87"),
ID: 66062,
Name: 'Hostname2',
Identifier: 'hostname_example2'
},
{
_id: ObjectId("6253492b634e9f3970c5dc88"),
ID: 56415,
Name: 'Hostname3',
Identifier: 'hostname_example3'
},
{
_id: ObjectId("6253492b634e9f3970c5dc89"),
ID: 84576,
Name: 'Hostname4',
Identifier: 'hostname_example4'
}
]
collection2 =
[
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'flowHostname1',
flowIPd: '11.11.11.11'
},
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example2',
flowIPd: '12.12.12.12'
},
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example3',
flowIPd: '13.13.13.13'
},
]
我需要这样的比较:
我想搜索-collections1-中标识符的“ flowhostname'-collections2-”的值。 (我的目标是在这里有两件事,1。等于2。包含) 在上面的示例中,我想获取以下数据。
result=
[
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example2',
flowIPd: '12.12.12.12'
},
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example3',
flowIPd: '13.13.13.13'
},
]
如果我以某种方式获得比赛对我来说就足够了。
我该如何使用Python的Pymongo进行操作?
I have two collections.
For example;
collection1:
[
{
_id: ObjectId("6253492b634e9f3970c5dc86"),
ID: 90448,
Name: 'Hostname1',
Identifier: 'hostname_example1'
},
{
_id: ObjectId("6253492b634e9f3970c5dc87"),
ID: 66062,
Name: 'Hostname2',
Identifier: 'hostname_example2'
},
{
_id: ObjectId("6253492b634e9f3970c5dc88"),
ID: 56415,
Name: 'Hostname3',
Identifier: 'hostname_example3'
},
{
_id: ObjectId("6253492b634e9f3970c5dc89"),
ID: 84576,
Name: 'Hostname4',
Identifier: 'hostname_example4'
}
]
collection2 =
[
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'flowHostname1',
flowIPd: '11.11.11.11'
},
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example2',
flowIPd: '12.12.12.12'
},
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example3',
flowIPd: '13.13.13.13'
},
]
I need a comparison like this:
I want to search value of 'flowHostname'-collections2- in identifier in -collections1-. (I'm aiming for 2 things here, 1. to be equal, 2. contains)
In the above example, I want to get the following data.
result=
[
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example2',
flowIPd: '12.12.12.12'
},
{
_id: ObjectId("625350ff09043895b2c3b2d3"),
flowSourceType: 'Unix',
flowHostname: 'hostname_example3',
flowIPd: '13.13.13.13'
},
]
It's enough for me if I get the matches somehow.
How can I do this using pymongo with python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将相等的
$或
条件放在第一个$ match
阶段的条件中。您可以使用$ limit:1
来提高性能。这是 mongo playground 供您参考。
Simply put the
$or
condition for equal and contains condition in the first$match
stage of subpipeline. You can use$limit: 1
to improve performance.Here is the Mongo playground for your reference.