MongoDB中怎么实现关系运算-创新互联

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中怎么实现关系运算问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联-成都网站建设公司行业资讯频道了解更多相关知识。


分享标题:MongoDB中怎么实现关系运算-创新互联
浏览地址:http://bzwzjz.com/article/ddjeig.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 营销型网站建设 企业网站设计 成都网站建设 成都营销网站建设 成都定制网站建设 企业网站建设 品牌网站建设 泸州网站建设 H5网站制作 成都品牌网站建设 成都网站建设公司 品牌网站建设 成都网站建设 网站建设方案 成都网站设计 响应式网站设计 高端网站设计推广 教育网站设计方案 成都商城网站建设 阿坝网站设计 成都网站设计 网站设计制作