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

mongodb查询嵌入式文档

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 14:58:35
Original
1018 people have browsed it

mongodb查询嵌入式文档 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: 直板

mongodb查询嵌入式文档

 

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:
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
Latest Issues
mongodb start
From 1970-01-01 08:00:00
0
0
0
linux - ubuntu14 error installing mongodb
From 1970-01-01 08:00:00
0
0
0
Use of symfony2 mongodb
From 1970-01-01 08:00:00
0
0
0
mongodb _id rename
From 1970-01-01 08:00:00
0
0
0
Parameter understanding of mongodb
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template