返回介绍

查询 find 的不等修饰符

发布于 12-09 12:55 字数 5393 浏览 0 评论 0 收藏 0

构造一些数据:

/**
 * 批量增加一些数据
 */
var student1 = {
    name: 'lisi',
    age: 10,
    sex: 1,
    hobby: {
        hobbyOne: '篮球',
        hobbyTwo: '足球',
        hobbyThree: '羽毛球'
    },
    skill: [],
    createTime: new Date()
};
var student2 = {
    name: 'wangwu',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '游泳',
        hobbyTwo: '健身',
        hobbyThree: '乒乓球'
    },
    skill: [],
    createTime: new Date()
};
var student3 = {
    name: 'xiaohong',
    age: 13,
    sex: 1,
    hobby: {
        hobbyOne: '瑜伽',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student4 = {
    name: 'xiaohua',
    age: 12,
    sex: 0,
    hobby: {
        hobbyOne: '体操',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student5 = {
    name: 'zhaoliu',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '音乐',
        hobbyTwo: '美术',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student6 = {
    name: 'xiaoli',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '体操',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student7 = {
    name: 'xiaoliu',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '钓鱼',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student8 = {
    name: 'zhangsan',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '足球',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student9 = {
    name: 'lihua',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '篮球',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var student10 = {
    name: 'xiaozhang',
    age: 12,
    sex: 1,
    hobby: {
        hobbyOne: '乒乓球',
        hobbyTwo: '跳舞',
        hobbyThree: '书法'
    },
    skill: [],
    createTime: new Date()
};
var db = connect('user');
var studentArr = [student1, student2, student3, student4, student5, student6, student7, student8, student9, student10];
db.student.insert(studentArr);
print('The data was inserted successfully');
> load('./demo8.js')
connecting to: mongodb://127.0.0.1:27017/user
MongoDB server version: 3.6.3
The data was inserted successfully

简单查找

// 查找 hobbyOne 是篮球的数据
> db.student.find({"hobby.hobbyOne": "篮球"})
{ "_id" : ObjectId("5cbaeef75328a836aca6e026"), "name" : "lisi", "age" : 10, "sex" : 1, "hobby" : { "hobbyOne" : "篮球", "hobbyTwo" : "足球", "hobbyThree" : "羽毛球" }, "skill" : [ ], "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "_id" : ObjectId("5cbaeef75328a836aca6e02e"), "name" : "lihua", "age" : 12, "sex" : 1, "hobby" : { "hobbyOne" : "篮球", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" }, "skill" : [ ], "createTime" : ISODate("2019-04-20T10:05:43.190Z") }

筛选字段

find 的第二个参数可以用来指定返回的字段。

// 只显示 name 和 hobby
> db.student.find( {"hobby.hobbyOne": "篮球"}, {name: true, hobby: true, _id: false} )
{ "name" : "lisi", "hobby" : { "hobbyOne" : "篮球", "hobbyTwo" : "足球", "hobbyThree" : "羽毛球" } }
{ "name" : "lihua", "hobby" : { "hobbyOne" : "篮球", "hobbyTwo" : "跳舞", "hobbyThree" : "书法" } }

不等修饰符

  • 小于($lt):英文全称 less-than
  • 小于等于($lte):英文全称 less-than-equal
  • 大于($gt):英文全称 greater-than
  • 大于等于($gte):英文全称 greater-than-equal
  • 不等于($ne):英文全称 not-equal
// 查年龄大于等于 12,且小于等于 14 的学生
> db.student.find(
... {age:{$lte:14, $gte: 12}},
... {name: true, age: true, _id: false}
... )
{ "name" : "wangwu", "age" : 12 }
{ "name" : "xiaohong", "age" : 13 }
{ "name" : "xiaohua", "age" : 12 }
{ "name" : "zhaoliu", "age" : 12 }
{ "name" : "xiaoli", "age" : 12 }
{ "name" : "xiaoliu", "age" : 12 }
{ "name" : "zhangsan", "age" : 12 }
{ "name" : "lihua", "age" : 12 }
{ "name" : "xiaozhang", "age" : 12 }

日期筛选

> var startTime = new Date('01/01/2019')
// 筛选创建日期大于 2019 年 1 月 1 日的数据
> db.student.find( {createTime:{$gt:startTime}}, {name: true, age: true, createTime: true, _id: false} )
{ "name" : "lisi", "age" : 10, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "wangwu", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "xiaohong", "age" : 13, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "xiaohua", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "zhaoliu", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "xiaoli", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "xiaoliu", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "zhangsan", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "lihua", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }
{ "name" : "xiaozhang", "age" : 12, "createTime" : ISODate("2019-04-20T10:05:43.190Z") }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文