• MongoDB常用命令


    一. 基本操作

    1.2 增

    >db.test.insert({x:"1"}) //test 集合如果不存在,则创建
    >for (var i = 1; i <= 25; i++) db.testData.insert( { x : i } )

    1.2 查

    >db.test.find()
    >var c = db.testData.find()
    >while ( c.hasNext() ) printjson( c.next() )

    >db.test.find( { x : "7" } )
    >db.test.find({x:"1"},{"_id":0,"x":1}) //不包含_id
    >db.test.find().limit(3)
    >db.users.find( { age: { $gt: 18 } }, { name: 1, address: 1 } ).limit(5).sort({age:1})
    >db.test.find( { x: { $in: [ 1, 3,'hello' ] } } )
    >db.inventory.find( { x: '1', $or: [ { qty: { $gt: 100 } }, { price: { $lt: 9.95 } } ]} )

    start=new Date("2014-06-25 00:00:00")
    >db.test.find({"insert_time":{"$lt":start}})

    1.3 改

    update(Document query,Document update,Document options)
    query:查询条件
    update:更新规则
    $inc,$mul,$rename,$setOnInsert,$set/$unset,$min/$max,$currentDate
    options:选项

    key值类型说明
    upsert boolean(false) 如果不存在,是否创建。
    multi boolean(false) 是否对符合条件的doc全部更新


    默认只更新一条。

    • 将adu的年龄增加10
    >db.test.update({"name":"adu"},{"$inc":{"age":10}})

    • 将所有name为"adu"的年龄加10
    >db.test.update({"name":"adu"},{"$inc":{"age":10}},{"multi":true})

    • 将adu的年龄加10,并将sex设置为"male"
    >db.test.update({"name":"adu"},{"$inc":{"age":10},"$set":{"sex":"male"}})

    • 完全替换(没有使用$操作)
    update({"name":"adu"},{"age":29,"name":"test"})
    update({"name":"adu"},{"age":29,"name":"test"},{"upsert":true})

    1.4 查找并修改

    findAndModify(Document query,Document sort,boolean remove,Document update,boolean new,Document fields,boolean upsert)
    修改并返回单个doc.默认返回修改前的doc,如果想要修改后的,设置new为true.
    query:查询条件
    sort:满足查询条件的结果进行排序,并只会对排在首位的doc进行修改
    remove:是否删除,默认false。remove和update必须指定一个。
    update:更新规则
    new:是否返回修改后的doc,默认为false。对于remove操作,此选项会被略过。
    fileds:返回doc的字段筛选。如:{"name":1,"age":1}
    upsert:不存在的doc,是否直接插入,默认false.

    • 
    >db.people.findAndModify({
    query: { name: "Tom", state: "active", rating: { $gt: 10 } },
    sort: { rating: 1 },
    update: { $inc: { score: 1 } }
    })

    >db.people.findAndModify({
    query: { name: "Gus", state: "active", rating: 100 },
    sort: { rating: 1 },
    update: { $inc: { score: 1 } },
    upsert: true
    })

    db.people.findAndModify({
    query: { name: "Pascal", state: "active", rating: 25 },
    sort: { rating: 1 },
    update: { $inc: { score: 1 } },
    upsert: true,
    new: true
    })

    • 删除排名最低的那位活跃用户
    >db.people.findAndModify(
    {
    query: { state: "active" },
    sort: { rating: 1 },
    remove: true
    }
    )

    1.5 删

    >db.test.remove({})
    • 删除多条
    >db.products.remove( { qty: { $gt: 20 } })

    • 删除一条
    >db.products.remove( { qty: { $gt: 20 } }, true )

    二. 索引

    2.1 查询

    getIndexes()

    2.2 创建

    ensureIndex(Document keys, Document options)
    keys:key为索引字段,value为1(升序)或-1(降序)
    value:

    key值类型说明
    background boolean(false) 是否后台运行。如果后台,则mongodb在创建过程中会继续服务
    unique boolean(false) 是否唯一索引。




    • 单列升序(非唯一)
    >db.test.ensureIndex({"name":1})

    • 联合索引(name升序,age降序)
    >db.test.ensureIndex({"name":1,"age":-1})

    • 联合唯一索引
    >db.test.ensureIndex({"name":1,"age":-1},{"unique":true})

    2.3 修改

    mongodb不支持修改索引,如果想要修改就只能先删除再创建。

    2.4 删除

    • 删除索引
    dropIndex(index)
    index为string(索引名)或document。
    >dropIndex("nameIdex")
    >dropIndex({"name":1})

    • 删除所有索引(除了_id)
    >dropIndexes()

  • 相关阅读:
    RabbitMQ知识点整理12-消费端的确认与拒绝
    RabbitMQ知识点整理11-消费消息
    RabbitMQ知识点整理0-准备工作和记录
    设计模式-23种设计模式
    设计原则-6大设计原则
    super在python 2.7和Python3中的使用
    rest-framework 视图类源码分析
    celery 组件在django环境应用
    rest framwork 4 分页功能
    rest framework 学习 序列化
  • 原文地址:https://www.cnblogs.com/waterystone/p/5086202.html
Copyright © 2020-2023  润新知