iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >find详解
  • 539
分享到

find详解

2024-04-02 19:04:59 539人浏览 八月长安
摘要

等价查询某个数组:db.getCollection("test").find(     {         tags: ["Good","book","it","program"]      } );(//

find详解

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的值。可用于文档数值字段,也可以用于数组字段
您可能感兴趣的文档:

--结束END--

本文标题: find详解

本文链接: https://www.lsjlt.com/news/41961.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作