简体   繁体   English

pymongo如何查询嵌套文档以忽略键但得到它的值?

[英]pymongo how to query nested document to ignore the key but get its value?

I have a documents inserted in mongoDB in the format as below: 我有一个插入mongoDB的文件,格式如下:

{'Name':{'Surname':{'JON':{'AGE':10}}}}

In the example above I want to build a query to ignor the 'JON' but fetch AGE value of all users. 在上面的示例中,我想构建一个查询来忽略'JON'但是获取所有用户的AGE值。

I tried like: 我尝试过:

db.names.find({'Name.Surname':{$regex:'.'}}) but didn't work....

What I'm looking for is something like: 我正在寻找的是:

db.names.find({'Name.Surname.<matchanything>.AGE':{$gt:0}})

I think the document scheme you have is going to make it hard for you to query, even if MongoDB supported wildcard matches in this manner (it currently does not). 我认为你的文档方案会让你难以查询,即使MongoDB以这种方式支持通配符匹配(目前还没有)。 Basically it would come down to doing a lot of table scans. 基本上它会归结为做很多表扫描。

Is there a reason for having Age as a property of surname? 将Age作为姓氏财产是否有理由?

An alternative would be to have a document structure more along the lines of 另一种方法是使文档结构更符合

{
    "_id" : ObjectId(),
    "Name" : "something",
    "Surname" : "JON",
    "Age" : 10
}

Which would be more handy for queries: 哪个查询会更方便:

db.user.find({"Name":"something"})
db.user.find({"Age":{"$gte":5}})

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

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