MongoDB中怎么实现关系运算

发布时间:2021-07-16 16:50:13 作者:Leah
来源:亿速云 阅读:92

MongoDB中怎么实现关系运算,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

在MongoDB里面支持的关系查询操作:大于($gt),小于($lt),大于等于($gte),小于等于($lte),不等于($ne),等于(key:value,$eq)。

范例:定义一个员工信息集合
db.emp.drop();
db.emp.insert({"name":"赵一","sex":"男","age":30,"sal":1000,"loc":"北京"});
db.emp.insert({"name":"钱二","sex":"女","age":22,"sal":5000,"loc":"上海"});
db.emp.insert({"name":"孙三","sex":"男","age":40,"sal":2000,"loc":"深圳"});
db.emp.insert({"name":"李四","sex":"女","age":30,"sal":7000,"loc":"北京"});
db.emp.insert({"name":"周五","sex":"女","age":30,"sal":6400,"loc":"北京"});
db.emp.insert({"name":"吴六","sex":"男","age":30,"sal":2500,"loc":"重庆"});
db.emp.insert({"name":"郑七","sex":"女","age":50,"sal":4700,"loc":"成都"});
db.emp.insert({"name":"王八","sex":"男","age":35,"sal":8000,"loc":"北京"});

> db.emp.find().pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "赵一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be27"),
        "name" : "钱二",
        "sex" : "女",
        "age" : 22,
        "sal" : 5000,
        "loc" : "上海"
}
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孙三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}
{
        "_id" : ObjectId("599108423268c8e84253be29"),
        "name" : "李四",
        "sex" : "女",
        "age" : 30,
        "sal" : 7000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2a"),
        "name" : "周五",
        "sex" : "女",
        "age" : 30,
        "sal" : 6400,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2b"),
        "name" : "吴六",
        "sex" : "男",
        "age" : 30,
        "sal" : 2500,
        "loc" : "重庆"
}
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "郑七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

范例:查询姓名为孙三的信息
> db.emp.find({"name":"孙三"}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孙三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}

范例:查询性别为男的人员信息
> db.emp.find({"sex":"男"}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "赵一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孙三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}
{
        "_id" : ObjectId("599108423268c8e84253be2b"),
        "name" : "吴六",
        "sex" : "男",
        "age" : 30,
        "sal" : 2500,
        "loc" : "重庆"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

范例:查询年龄大于30的人员
> db.emp.find({"age":{"$gt":30}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be28"),
        "name" : "孙三",
        "sex" : "男",
        "age" : 40,
        "sal" : 2000,
        "loc" : "深圳"
}
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "郑七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

范例:查询薪水大于等于4700的人员
> db.emp.find({"sal":{"$gte":4700}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be27"),
        "name" : "钱二",
        "sex" : "女",
        "age" : 22,
        "sal" : 5000,
        "loc" : "上海"
}
{
        "_id" : ObjectId("599108423268c8e84253be29"),
        "name" : "李四",
        "sex" : "女",
        "age" : 30,
        "sal" : 7000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2a"),
        "name" : "周五",
        "sex" : "女",
        "age" : 30,
        "sal" : 6400,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "郑七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
}

范例:查询姓名不是孙三的信息
> db.emp.find({"name":{"$ne":"孙三"}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "赵一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be27"),
        "name" : "钱二",
        "sex" : "女",
        "age" : 22,
        "sal" : 5000,
        "loc" : "上海"
}
{
        "_id" : ObjectId("599108423268c8e84253be29"),
        "name" : "李四",
        "sex" : "女",
        "age" : 30,
        "sal" : 7000,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2a"),
        "name" : "周五",
        "sex" : "女",
        "age" : 30,
        "sal" : 6400,
        "loc" : "北京"
}
{
        "_id" : ObjectId("599108423268c8e84253be2b"),
        "name" : "吴六",
        "sex" : "男",
        "age" : 30,
        "sal" : 2500,
        "loc" : "重庆"
}
{
        "_id" : ObjectId("599108423268c8e84253be2c"),
        "name" : "郑七",
        "sex" : "女",
        "age" : 50,
        "sal" : 4700,
        "loc" : "成都"
}
{
        "_id" : ObjectId("599108433268c8e84253be2d"),
        "name" : "王八",
        "sex" : "男",
        "age" : 35,
        "sal" : 8000,
        "loc" : "北京"
此时与之前最大的区别就在于,在一个JSON结构里面需要定义其他的JSON结构,并且这种风格在进行程序操作的时候依然如此。

关于MongoDB中怎么实现关系运算问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. RDBMS能实现的专门关系运算包括什么
  2. mongodb中怎么实现连表查询

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

mongodb

上一篇:MongoDB中怎么实现安全管理

下一篇:Web开发中客户端跳转与服务器端跳转有什么区别

相关阅读

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

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