Home > Database > Mysql Tutorial > mongodb查询嵌入式文档

mongodb查询嵌入式文档

WBOY
Release: 2016-06-07 17:45:29
Original
1167 people have browsed it

db.getCollection("mobiles").ensureIndex({ "params.name": 1, "params.value": 1 }); db.getCollection("mobiles").insert({ "_id": 1, "name": "ME525", "brand": "摩托罗拉", "params": [ {"name": "待机时间", "value": 200}, {"name": "外观设计", "va

db.getCollection("mobiles").ensureIndex({
    "params.name": 1,
    "params.value": 1
});

db.getCollection("mobiles").insert({
    "_id": 1,  
    "name": "ME525",
    "brand": "摩托罗拉",
    "params": [
        {"name": "待机时间", "value": 200},
        {"name": "外观设计", "value": "直板"}
    ]
});

db.getCollection("mobiles").insert({
    "_id": 2,
    "name": "E7",
    "brand": "诺基亚",
    "params": [  
        {"name": "待机时间", "value": 500},
        {"name": "外观设计", "value": "滑盖"}
    ]
});
  

 

如果想查询待机时间大于100小时,网站空间,并且外观设计是直板的手机,香港空间,需要按照如下方式查询:

db.getCollection("mobiles").find({
    "params": {
        $all: [
            {$elemMatch: {"name": "待机时间", "value": {$gt: 100}}},
            {$elemMatch: {"name": "外观设计", "value": "直板"}}
        ]
    }
});
  

 

注:查询中用到的$all,美国空间,$elemMatch等高级用法的详细介绍请参考官方文档中相关说明。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template