温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

find详解

发布时间:2020-07-16 16:41:50 阅读:311 作者:梁十八 栏目:关系型数据库
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

find详解

find详解

find详解

等价查询某个数组:

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

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

查询数组里的某一个值:

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

查询有四个元素的数组:

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

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

db.getCollection("test").insert(
    [
        {_id2222, toynull},
        {_id1112}
    ]
);
db.getCollection("test").find(
    {_id2222, toynull}
);
db.getCollection("test").find(
    toynull
); //报错
db.getCollection("test").find(
    {_id2222, toy: {$existstrue}}
); //找出来了当前这条
db.getCollection("test").find(
    {toy: {$existstrue}}
); //找出来了当前这条
db.getCollection("test").find(
    {toy: {$existsfalse}}
); //找出所有没有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: {$gt3$lt33}
    }
); //查询价格范围大于3小子33的值。可用于文档数值字段,也可以用于数组字段

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×