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

mongodb中如何匹配数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 14:58:491589browse

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]}});

Statement:
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
Previous article:搭建Hive的图形界面Next article:ubuntu install mongodb