zoukankan      html  css  js  c++  java
  • MongoDB 文档的查询和插入操作

    MongoDB是文档型数据库,有一些专门的术语,和关系型DB相似,但也有差异,例如,Collection类似于关系型DB的Table,document类似于row,key/value pair类似于column。document 是使用{}为边界,一个Key/Value对使用“:”分割,key/value pair之间使用“,”分割,例如

    user={ name:"sue",age:24 }

    MongoDB中能够定义document 数组,这对于批量更新和批量插入操作非常有用。

    复制代码
    userArray=
    [
      { name:"sue",age:24 },
      { name:"joe",age:25 },
      { name:"pei",age:32 }
    ]
    复制代码

    MongoDB有一个test db,在学习MongoDB时,可以使用use 命令切换到test db。

    use test

    一,插入操作

    MongoDB的插入操作是将document插入到collection中,MongoDB提供三种插入函数db.collection.insert,db.collection.insertOne和db.collection.insertMany,insert函数能够插入单个doc,也能插入doc的数组。

    1,插入单个document

    user={ name:"test1", age:22}
    db.users.insert(user)
    db.users.insert( { name:"test1", age:22} ) db.users.insertOne( {name:"test1", age:22} )

    2,批量插入document

    复制代码
    user1={ name:"t1", age:21}
    user2={ name:"t2", age:22}
    user3={ name:"t3", age:23}
    
    db.users.insert([user1,user2,user3])
    db.users.insertMany([user1,user2,user3])
    
    --or 
    userArray=[user1,user2,user3]
    db.users.insertMany([user1,user2,user3])
    db.users.insert([user1,user2,user3])
    复制代码

    二,查找操作

    查询查询的语法

    db.collection.find( <query filter>, <projection> )

    query filter是查询的过滤条件,projection是从document中查找特定的key/value pair,类似于关系型DB的where子句和select子句。

    不加任何query filter时,将查询所有的document

    db.users.find()
    db.users.find({})

    1,Query filter

    1.1 在query filter中,“=”使用 key/value pair,或 $eq 表示,两者是等价的

    {field: <value>}
    { <field>: { $eq: <value> } }

    例如,查询age=21的所有user

    db.users.find({age:21})
    db.users.find({age:{$eq:21}})

    不等式使用$ne表示

    {field: {$ne: value} }

    例如,查看age<>21的所有user

    db.users.find({age:{$ne:21}})

    1.2 “>”,“>=”,“<”“<=” 分别使用 $gt,$gte,$lt 和 $lte 表示,格式是

    { field: { $lt: value} }
    { field: { $gt: value} }
    { field: { $lte: value} }
    { field: { $gte: value} }

    例如,分别查询age<22 , age>22,age<=22,age>=22的所有user

    db.users.find({age:{$lt:22}})
    db.users.find({age:{$gt:22}})
    db.users.find({age:{$lte:22}})
    db.users.find({age:{$gte:22}})

    1.3 逻辑或算符使用 $or 表示,格式是

    { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }

    例如,查询age=21或age=22的所有user

    db.users.find({$or:[{age:21},{age:22}]})

    1.4,逻辑与运算符使用“,” ,或者$and 表示,格式是

    { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }

    例如,查询name是“t1”,并且 age=21的所有user

    db.users.find({$and:[{name:"t1"},{age:21}]})
    db.users.find({name:"t1",age:21})

    1.5,在范围内查询,使用 $in 表示,不在范围内,使用$nin表示,格式是

    { field: { $in: [<value1>, <value2>, ... <valueN> ] } }
    { field: { $nin: [<value1>, <value2>, ... <valueN> ] } }

    例如,查询age是21 或 22的所有user,查询age不是21 和 22的所有user

    db.users.find({age:{$in:[21,22]}})
    db.users.find({age:{$nin:[21,22]}})

    1.6 其他运算符,请参考官方文档《Query and Projection Operators
    2,projection

    projection是指定collection中的哪些field需要返回,默认情况下,会返回所有的filed。如果没有指定"_id"的projection,那么结果集返回"_id",详细信息参考《Project Fields to Return from Query

    a query projection to specifies which fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.

    { field1: <value>, field2: <value> ... }

    示例:

    db.users.find({$and:[{name:"t1"},{age:21}]},{name:1,_id:0})
    db.users.find({$and:[{name:"t1"},{age:21}]},{name:0,_id:0})

    3,返回doc 或 iterator

    db.collection.find()返回的是cursor,而db.collection.findOne()返回的是single doc。通过db.collection.find() 返回cursor时,必须使用 var 关键字定义cursor,如果没有显式使用var,那么cursor会自动迭代20次,以显示前20个doc。

    In the mongo shell, when you assign the cursor returned from the find() method to a variable using the var keyword, the cursor does not automatically iterate. However,if the returned cursor is not assigned to a variable using the var keyword, then the cursor is automatically iterated up to 20 times [1] to print up to the first 20 documents in the results.

    step1,使用var 关键字定义cursor

    var us =db.users.find()

    step2,迭代cursor,将doc以json格式显示

    while (us.hasNext())
    {
    print(tojson(us.next()));
    }

    step3,使用foreach函数

    var us=db.users.find();
    us.forEach(printjson);

    参考文档:

    Query and Projection Operators

    MongoDB CRUD Operations

    Iterate a Cursor in the mongo Shell

  • 相关阅读:
    1082 射击比赛 (20 分)
    1091 N-自守数 (15 分)
    1064 朋友数 (20 分)
    1031 查验身份证 (15 分)
    1028 人口普查 (20 分)
    1059 C语言竞赛 (20 分)
    1083 是否存在相等的差 (20 分)
    1077 互评成绩计算 (20 分)
    792. 高精度减法
    791. 高精度加法
  • 原文地址:https://www.cnblogs.com/zxtceq/p/7690952.html
Copyright © 2011-2022 走看看