MongoDB之数据查询(嵌套集合)-创新互联

在M

创新互联建站从2013年创立,是专业互联网技术服务公司,拥有项目成都做网站、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元盐都做网站,已为上家服务,为盐都各地企业和个人服务,联系电话:028-86922220ongoDB数据库里面每一个集合数据可以继续保存其他的集合数据,例如有些人员保存家庭信息。
范例:增加数据
db.emp.insert({"name":"林A","sex":"男","age":22,"sal":8000,"loc":"北京","course":["语文","数学","英语","音乐","政治"],"parents":[{"name":"林A父亲","age":50,"job":"农民"},{"name":"林A母亲","age":49,"job":"工人"}]});
db.emp.insert({"name":"林B","sex":"男","age":30,"sal":8000,"loc":"北京","course":["语文","数学"],"parents":[{"name":"林B父亲","age":55,"job":"工人"},{"name":"林B母亲","age":52,"job":"农民"}]});
db.emp.insert({"name":"林C","sex":"男","age":35,"sal":8000,"loc":"北京","course":["语文","数学","英语"],"parents":[{"name":"林C父亲","age":60,"job":"工人"},{"name":"林C母亲","age":59,"job":"职员"}]});
db.emp.insert({"name":"林D","sex":"男","age":28,"sal":8000,"loc":"北京","course":["语文","数学","政治"],"parents":[{"name":"林D父亲","age":58,"job":"局长"},{"name":"林D母亲","age":54,"job":"处长"}]});
db.emp.insert({"name":"林E","sex":"男","age":40,"sal":8000,"loc":"北京","course":["语文","政治"],"parents":[{"name":"林E父亲","age":70,"job":"工人"},{"name":"林E母亲","age":68,"job":"局长"}]});
此时的内容是嵌套的集合,而这种集合的数据的判断只能通过“$elemMatch”来完成。
范例:查询出父母之中有局长的信息
> db.emp.find({"$and":[{"age":{"$gte":30}},{"parents":{"$elemMatch":{"job":"局长"}}}]}).pretty();
{
        "_id" : ObjectId("599148bf0184ff511bf02b95"),
        "name" : "林E",
        "sex" : "男",
        "age" : 40,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "语文",
                "政治"
        ],
        "parents" : [
                {
                        "name" : "林E父亲",
                        "age" : 70,
                        "job" : "工人"
                },
                {
                        "name" : "林E母亲",
                        "age" : 68,
                        "job" : "局长"
                }
        ]
}
由于这种查询的时候条件比较麻烦,所以如果可能,尽量别搞这么复杂。
当前标题:MongoDB之数据查询(嵌套集合)-创新互联
文章地址:http://bzwzjz.com/article/cdsegh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设 定制网站设计 成都网站建设 广安网站设计 成都网站建设流程 成都网站建设公司 成都响应式网站建设公司 成都网站建设 响应式网站建设 四川成都网站设计 四川成都网站制作 成都网站制作 成都企业网站设计 LED网站设计方案 成都网站建设公司 手机网站制作 宜宾网站设计 成都网站建设公司 梓潼网站设计 成都网站建设 成都网站建设 成都定制网站建设