Home > Database > Mysql Tutorial > mongodb中如何匹配数组

mongodb中如何匹配数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 14:58:49
Original
1587 people have browsed it

mongodb中如何匹配数组 假设: db.XXX.remove(); db.XXX.insert({names:[BuleRiver1, BuleRiver2, BuleRiver3]}); 下面的查询: db.XXX.find({names:BuleRiver1}); 将返回该条记录 如果要同时有BuleRiver1和BuleRiver2才返回呢? db.XXX.find({names:[BuleRi

mongodb中如何匹配数组

 

假设:

db.XXX.remove();

db.XXX.insert({"names":["BuleRiver1", "BuleRiver2", "BuleRiver3"]});

 

下面的查询:

db.XXX.find({"names":"BuleRiver1"});

将返回该条记录

 

如果要同时有BuleRiver1和BuleRiver2才返回呢?

db.XXX.find({"names":["BuleRiver1", "BuleRiver2"]});

不会返回刚才的条目。

怎么返回呢?使用$all:

db.XXX.find({"names":{"$all":["BuleRiver1", "BuleRiver2"]}})

 

如果要返回names数组长度为3的条目呢?使用$size

db.XXX.find({"names":{"$size":3}});

如果想要返回该数组的前2项呢?使用$slice

db.XXX.find({"names":{"$slice":2}});

 

返回后两条:

db.XXX.find({"names":{"$slice":-2}});

从第2条开始,返回3个条目:

db.XXX.find({"names":{"$slice":[2, 3]}});

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
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