zoukankan      html  css  js  c++  java
  • mongodb使用

    1、安装:下载tgz压缩包后解压可直接使用(注意区分版本,下面是linux版本)

    http://www.runoob.com/mongodb/mongodb-linux-install.html

    2、作为服务器启动服务:

    在解压后目录的bin目录中有多个脚本,mongod为启动对外服务的脚本。默认27017端口。linux下数据库文件默认存储路径/data/db/,可以使用--dbpath在运行时修改该路径

    ./mongod
    ./mongod --dbpath /home/zhangsuosheng/data/

    3、作为客户端连接服务

    命令行连接

    //连接MongoDB并指定端口
    
    mongo 192.168.1.100:27017
    
    //连接到指定的MongoDB数据库
    
    mongo 192.168.1.100:27017/test
    
    //指定用户名和密码连接到指定的MongoDB数据库(登录)
    
    mongo 192.168.1.200:27017/test -u user -p password

    命令行常用命令

    help 帮助

    show dbs 显示所有数据库

    use [数据库名] 切换到指定数据库

    show collections 显示数据库内所有集合

    db.[集合名].find() 显示集合内所有文档

    其他命令:

    查询age = 22的记录
    db.userInfo.find({"age": 22});
    查询age > 22的记录
    db.userInfo.find({age: {$gt: 22}});
    查询age < 22的记录
    db.userInfo.find({age: {$lt: 22}});
    查询age >= 25的记录
    db.userInfo.find({age: {$gte: 25}});
    查询age <= 25的记录
    db.userInfo.find({age: {$lte: 25}});
    查询age >= 23 并且 age <= 26
    db.userInfo.find({age: {$gte: 23, $lte: 26}});
    查询name中包含 mongo的数据
    db.userInfo.find({name: /mongo/});
    查询name中以mongo开头的
    db.userInfo.find({name: /^mongo/});
    查询指定列name、age数据
    db.userInfo.find({}, {name: 1, age: 1});
    当然name也可以用true或false,当用ture的情况下河name:1效果一样,如果用false就是排除name,显示name以外的列信息。
    查询指定列name、age数据, age > 25
    db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
    按照年龄排序
    升序:db.userInfo.find().sort({age: 1});
    降序:db.userInfo.find().sort({age: -1});
    查询name = zhangsan, age = 22的数据
    db.userInfo.find({name: 'zhangsan', age: 22});
    查询前5条数据
    db.userInfo.find().limit(5);
    查询10条以后的数据
    db.userInfo.find().skip(10);
    查询在5-10之间的数据
    db.userInfo.find().limit(10).skip(5);
    可用于分页,limit是pageSize,skip是第几页*pageSize
    or 查询
    db.userInfo.find({$or: [{age: 22}, {age: 25}]});
    查询第一条数据
    db.userInfo.findOne();
    db.userInfo.find().limit(1);
    查询某个结果集的记录条数
    db.userInfo.find({age: {$gte: 25}}).count();
    如果要返回限制之后的记录数量,要使用count(true)或者count(非0) 
    db.users.find().skip(10).limit(5).count(true);
    按照某列进行排序
    db.userInfo.find({sex: {$exists: true}}).count();

    可视化连接工具:robomongo(robo3t)

    下载,解压缩后直接使用

    https://robomongo.org/

    4、ORM——mongoengine

    文档:

    http://docs.mongoengine.org/guide/querying.html

    连接:

    connect(
            db='DMS_gaze',
            username='houzhe',
            password='houzhe',
            host='172.16.30.205',
            port=27017,
        )

    定义一个表(collection):

    from mongoengine import *
    
    
    class Persons(Document):  # mapping into a collection
        pid = StringField(required=True, unique=True)
        sex = BooleanField(required=True)    #True means male and False means female
    
    class Index(Document):  #img_id <-> pid
        img_id = IntField(required=True, unique=True)
        person = ReferenceField(Persons,reverse_delete_rule=CASCADE)
        cam_num = IntField(required=True)
    
    
    class PriImageUrls(Document):   #primal images
        img = ReferenceField(Index,reverse_delete_rule=CASCADE)    #img_id
        url = StringField(required=True, unique=True)

    ReferenceField为外键

    如果以Persons表为外键创建一个Index表(上面黄底色),则创建一条Index类型的文档时则需要传入一个Persons对象作为初始化参数

    如果同时设置reverse_delete_rule=CASCADE,则一个Persons对象被从Persons集合中删除时,其对应的Index对象会自动被删除(外键约束)

    创建一个文档

    a_new_person=Persons(pid=pid,sex=True)
    a_new_person.save()

    过滤文档(返回值类型为QueryDict,列表)

    query_result_person_list=Persons.objects() # 获取Persons集合中的所有文档
    query_result_person_list=Persons.objects(pid=222) # 获取Persons集合中pid=222的文档

    多条件过滤文档

    query_result_left_gaze_label_list=LeftGazeLabel.objects(Q(lo__gt=minlo) & Q(lo__lt=maxlo) & Q(la__gt=minla) & Q(la__lt=maxla))

    下面为常用的查询操作符,使用  字段加双下划线加操作符名的方式使用这些操作符,如上面红字所示

    Q()&Q()&Q()使用多条件查询,如上面黄底色所示 

    删除一个文档(不需要save)

    query_result_person_list=Persons.objects(pid=222)
    query_result_person_list[0].delete()

    更新一个文档(不需要save)

    query_result_person_list=Persons.objects(pid=222)
    query_result_person_list[0].update(sex=False)
  • 相关阅读:
    关于javascript app框架的几篇文章
    php 开发笔记
    php 图片处理扩展(windows平台)
    [javascript]最短 domready
    javascript小陷阱
    HTML DOM whiteSpace
    40岁后才明白的道理:人一生奋斗余地很有限转载
    [学习笔记]lca倍增
    Django model 字段类型及选项解析转载
    PythonPEP8 风格规范指南
  • 原文地址:https://www.cnblogs.com/zealousness/p/9898438.html
Copyright © 2011-2022 走看看