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字段既有數字,又有數組時的查詢和投影選項
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章