MongoDB C100DEV 2.CRUD

版权信息

  • 本文为CSDN 博主luopotaotao原创,转载请注明出处
  • 欢迎加群交流探讨 218368963【MongoDB C100DEV】

CRUD

这部分是重点,有十几二十来题吧

实际考试中,重点有

  • 语法的检查
  • 数组的查询
  • 带multi,upsert选项的update操作
  • 比较操作符(会明确给出是gt, lt还是gte, lte,不会有边界模糊的情况)
  • 条件结合in, and,$or
  • skip和limit

需要具备:

  • 明白MongoDB的crud语言
  • 熟悉并掌握常用的CRUD操作
  • 知道MongoDB支持哪些数据类型
    考试不检查语法的记忆,但是需要能够分辨出给定查询的正确性.
    也要知道哪些查询参数是必要的,哪些是可选的,如何使用这些参数

Create

  • 正确的使用insert,save,update,findAndModify命令创建新的文档
  • 给出 insert 命令,指出其具体做了些什么
  • 如何执行批量插入
  • 理解_id字段的唯一性限制及其在CURD中的含义
  • 理解ObjectIds如何创建和使用

Read

  • 正确的使用query,projection和可选参数
  • 排序查询结果
  • 所有的match和projection操作符
  • 读译正则表达式查询
  • 查询数组

Update

  • 正确的使用save,update,findAndModify命令修改存在的文档
  • 这句话不明白
    Distinguish which parameter finds the documents to change, which mutates them
  • 解释update操作符的行为
  • 识别什么时候upserts和db.collections.save()将会插入新的文档

Delete

  • 删除集合
  • 构建删除命令来删除指定文档

问题示例

1.Consider the following documents:

{ "_id" : 1, "a" : 1, "b" : 1 }
{ "_id" : 2, "a" : 2, "b" : 3 }
{ "_id" : 3, "a" : 3, "b" : 6 }
{ "_id" : 4, "a" : 4, "b" : 10 }
{ "_id" : 5, "a" : 5, "b" : 15 }
You perform the following query:

db.stuff.update( { b : { $gte : 10 } },
                 { $set : { b : 15 } },
                 { multi : true, upsert : true } )
How many documents will be updated by the query?

a.0
b.1
c.2
d.3
e.5

2.Consider the following document:

> db.c.find()
{ "_id" : 12, b : [ 3, 5, 7, 2, 1, -4, 3, 12 ] }
Which of the following queries on the "c" collection will return only the first five elements 
of the array in the "b" field? E.g.,

Document you want returned by your query:

{ "_id" : 12, "b" : [ 3, 5, 7, 2, 1 ] }
a.db.c.find( { } , { b : [ 0, 1, 2, 3, 4, 5 ] } )
b.db.c.find( { } , { b : [ 0 , 5 ] } )
c.db.c.find( { } , { b : { $slice : [ 0 , 5 ] } } )
d.db.c.find( { } , { b : { $substr[ 0 , 5 ] } } )
e.db.c.find( { b : [ 0 , 5 ] } )

3.Consider the following example document from the sample collection.
All documents in this collection have the same schema.

{
  "_id" : 3,
  "a" : 7,
  "b" : 4
}
Which of the following queries will replace this with the document,

{
  "_id" : 7,
  "c" : 4,
  "b" : 4
}
a.db.sample.update( { "_id" : 3 } , { "_id" : 7 , "c" : 4 } )
b.db.sample.update( { "_id" : 3 } , { "$set" : { "_id" : 7 , "c" : 4 } } )
c.db.sample.update( { "_id" : 3 } , { "_id" : 7 , "c" : 4 , { "$unset" : [ "a" , "b" ] } } )
d.db.sample.update( { "_id" : 3 } , { "_id" : 7 , "c" : 4 } , { "justOne" : true } )
e.This operation cannot be done with a single query.

4.Which of the documents below will be retrieved by the following query?
Assume the documents are stored in a collection called “sample”. Check all that apply.
db.sample.find( { “or" : [ { "a" : { " in" : [ 3, 10] } }, { "b" : { "$lt” : 2 } } ] } )

a. { "_id" : 1, "a" : 0, "c" : 0, "b" : 2 }
b. { "_id" : 2, "a" : 2, "c" : 0, "b" : 1 }
c. { "_id" : 3, "a" : 4, "c" : 0, "b" : 14 }
d. { "_id" : 4, "a" : 5, "c" : 0, "b" : 17 }
e. { "_id" : 5, "a" : 3, "c" : 0, "b" : 12 }
f. { "_id" : 6, "a" : 1, "c" : 1, "b" : 5 }
g. { "_id" : 7, "a" : 8, "c" : 1, "b" : 7 }
h. { "_id" : 8, "a" : 11, "c" : 1, "b" : 0 }
i. { "_id" : 9, "a" : 17, "c" : 1, "b" : 1 }
j. { "_id" : 10, "a" : 3, "c" : 1, "b" : 1 }
You perform the following operation in the shell:

db.foo.insert( { } );
What gets inserted?

a.An empty document
b.A document with an _id assigned to be an ObjectId
c.A document that matches the collection's existing schema, but with null fields
d.No document will be inserted; an error will be raised
e.A document will be inserted with the same _id as the last document inserted

Answers to Sample Problems

1.b
2.c
3.e
4.b, e, h, i, j
5.b

例题(大概意思和方向,非原题)

1.集合coll中有以下文档,执行db.coll.update({a:{$gte:5}},
{$set:{b:15}},
{multi:true,upsert:true})

    a. {_id:id0,a:1,b:3}   
    b. {_id:id1,a:5,b:5}
    c. {_id:id2,a:10,b:15}
    问题1:哪些文档被查询到 b,c
    问题2:有多少条记录会被插入 0 因为查询到了记录,upsert选项不做插入操作
    问题3:有多少条记录会被更新 1 因为c中b的值就是15,因此不会被更新

2.集合中以下内容,执行查询db.coll.find({a:{$gte:5}},{b:1,_id:0}),

    a. {_id:id0,a:1,b:3}
    b. {_id:id1,a:5,b:5}
    c. {_id:id2,a:10,b:15}
    c. {_id:id3,a:10,b:[3,15,20]}
    哪些记录会被查询到 b,c,d 
    主要考察b字段既有数字,又有数组时的查询和投影选项
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章