重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

find详解

find详解

成都创新互联"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!成都创新互联具备承接各种类型的成都做网站、网站设计项目的能力。经过10年的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。

find详解

find详解

等价查询某个数组:

db.getCollection("test").find(
    {
        tags: ["good","book","it","program"] 
    }
);

(//连顺序都要是存进去的顺序,不能变)

查询数组里的某一个值:

db.getCollection("test").find(
    {
        tags: "good"
    }
);

查询有四个元素的数组:

db.getCollection("test").find(
    {
        tags: {$size: 4}
    }
);

查询有或没有指定字段的,指定字段为null的情况:

db.getCollection("test").insert(
    [
        {_id: 2222, toy: null},
        {_id: 1112}
    ]
);
db.getCollection("test").find(
    {_id: 2222, toy: null}
);
db.getCollection("test").find(
    toy: null
); //报错
db.getCollection("test").find(
    {_id: 2222, toy: {$exists: true}}
); //找出来了当前这条
db.getCollection("test").find(
    {toy: {$exists: true}}
); //找出来了当前这条
db.getCollection("test").find(
    {toy: {$exists: false}}
); //找出所有没有toy这个字段的

查找返回值游标:

db.getCollection("test").find().forEach(function(item) {
    print(item.name, item.price, item.tags);
});

limit,skip方法:

db.getCollection("test").find().limit(1)
db.getCollection("test").find().skip(2)

$in的查询:

db.getCollection("test").find(
    {
        _id: {
            $in: {12, objectId("56970120abt538296thg0y6")}
        }
    }
); //查找_id等于12或objectId("56970120abt538296thg0y6")的文档记录

($in 用于不同文档指定同一个Key 进行或条件匹配, $or 可以指定多个Key 或条件匹配。)


区间查找:

db.getCollection("test").find(
    {
        price: {$gt: 3, $lt: 33}
    }
); //查询价格范围大于3小子33的值。可用于文档数值字段,也可以用于数组字段

文章名称:find详解
文章来源:http://cqcxhl.com/article/pocihh.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP