九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
《MongoDB高手課》學(xué)習(xí)記錄(第十天)

踩坑記錄

可能是之前的理解有問(wèn)題,昨天看了一下官方文檔,通過(guò)brew是可以直接安安裝MongoDB Community的最新版本的。當(dāng)然企業(yè)版本是沒(méi)啥辦法,還得下載壓縮包,然后自己配置。

安裝過(guò)程

需要的環(huán)境

  1. 4.2 版本的MongoDB只支持 10.12 版本以上的MacOS。
  2. 需要安裝 xcode
  3. 當(dāng)然還需要安裝 Homebrew
  4. 需要安裝 gcc
  5. 文檔地址

安裝過(guò)程

# 首先注冊(cè) MongoDB Homebrew Tap$> brew tap mongodb/brew# 現(xiàn)在我能查到的版本如下$> brew search mongodb-community==> Formulaemongodb/brew/mongodb-community                           mongodb/brew/mongodb-community@3.2                       mongodb/brew/mongodb-community@3.6mongodb/brew/mongodb-community-shell                     mongodb/brew/mongodb-community@3.4                       mongodb/brew/mongodb-community@4.0# 默認(rèn)沒(méi)有打Tag的版本是4.2.2$> brew info mongodb-communitymongodb/brew/mongodb-community: stable 4.2.2High-performance, schema-free, document-oriented databasehttps://www.mongodb.com/Not installedFrom: https://github.com/mongodb/homebrew-brew/blob/master/Formula/mongodb-community.rb==> CaveatsTo have launchd start mongodb/brew/mongodb-community now and restart at login:  brew services start mongodb/brew/mongodb-communityOr, if you don't want/need a background service you can just run:  mongod --config /usr/local/etc/mongod.conf  # 開(kāi)始安裝$> brew install mongodb-community==> CaveatsTo have launchd start mongodb/brew/mongodb-community now and restart at login:  brew services start mongodb/brew/mongodb-communityOr, if you don't want/need a background service you can just run:  mongod --config /usr/local/etc/mongod.conf==> Summary?  /usr/local/Cellar/mongodb-community/4.2.2: 21 files, 274.5MB, built in 27 minutes 20 seconds# 啟動(dòng)服務(wù)$> brew services start mongodb/brew/mongodb-community==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

安裝位置信息

  • 程序目錄:/usr/local/Cellar/mongodb-community
  • 配置文件:/usr/local/etc/mongod.conf
  • 系統(tǒng)日志:/usr/local/var/log/mongodb
  • 數(shù)據(jù)目錄:/usr/local/var/mongodb

第十天

今日復(fù)習(xí)內(nèi)容為CRUD操作,直接在Shell操作。都是官網(wǎng)的例子哈。
地址在這里:參考文檔

準(zhǔn)備練手?jǐn)?shù)據(jù)

> db.inventory.insertMany([...    { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },...    { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },...    { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },...    { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },...    { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }... ]);{    "acknowledged" : true,    "insertedIds" : [        ObjectId("5e06f1f5c01e4d7f78f96cc9"),        ObjectId("5e06f1f5c01e4d7f78f96cca"),        ObjectId("5e06f1f5c01e4d7f78f96ccb"),        ObjectId("5e06f1f5c01e4d7f78f96ccc"),        ObjectId("5e06f1f5c01e4d7f78f96ccd")    ]}

查詢操作

查詢所有記錄

select * from inventory

> db.inventory.find( {} ){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }> db.inventory.find(){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

我發(fā)現(xiàn),傳不傳 {} 空對(duì)象,結(jié)果都一樣哈。

指定查詢條件

SELECT * FROM inventory WHERE status = "D"
對(duì)應(yīng)的MQL如下

> db.inventory.find({"status": "D"}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }

SELECT * FROM inventory WHERE status in ("A", "D")
對(duì)應(yīng)的MQL如下:

> db.inventory.find({status: {$in:["A","D"]}}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

SELECT * FROM inventory WHERE status = "A" AND qty < 30
對(duì)應(yīng)的MQL如下

> db.inventory.find({status:"A", qty:{$lt:30}}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }>> db.inventory.find({$and:[{status:"A"},{qty:{$lt:30}}]}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

SELECT * FROM inventory WHERE status = "A" OR qty < 80
對(duì)應(yīng)的MQL如下

> db.inventory.find({$or:[{status: "A"},{qty:{$lt: 80}}]}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

SELECT * FROM inventory WHERE status = "A" AND ( qty < 30 OR item LIKE "p%")
對(duì)應(yīng)的MQL如下(這里面用到了正則表達(dá)式)

> db.inventory.find({$and:[{status: "A"},{$or:[{qty:{$lt: 30}},{item:/^p/}]}]}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }> db.inventory.find({$and:[{status: "A"},{$or:[{qty:{$lt: 30}},{item:{$regex:/^p/}}]}]}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

查詢內(nèi)嵌對(duì)象

比如我要查詢到這條記錄

{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

我要這么寫(xiě),也就是寫(xiě)上完整的條件

> db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } ){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }

換個(gè)順序也不行,必須按順序?qū)?/p>

> db.inventory.find( { size: { w:21, h: 14, uom: "cm" } } )

少寫(xiě)個(gè)字段不行

> db.inventory.find( { size: { h: 14, w: 21} } )

坑爹,那怎么搞
這樣,有了,“.” 操作符

> db.inventory.find({"size.w":21}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }>

加個(gè)條件

> db.inventory.find({"size.h":{$lte:10}}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }

再寫(xiě)復(fù)雜點(diǎn)

> db.inventory.find({"size.h":{$lte:10},"size.w":11,status:"A"}){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }

數(shù)組查詢

> db.inventory.insertMany([...    { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },...    { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },...    { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },...    { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },...    { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }... ]);{    "acknowledged" : true,    "insertedIds" : [        ObjectId("5e070270c01e4d7f78f96cce"),        ObjectId("5e070270c01e4d7f78f96ccf"),        ObjectId("5e070270c01e4d7f78f96cd0"),        ObjectId("5e070270c01e4d7f78f96cd1"),        ObjectId("5e070270c01e4d7f78f96cd2")    ]}> db.inventory.find(){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd2"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }

和文檔中對(duì)象的查詢一樣的問(wèn)題,要查一個(gè)數(shù)據(jù),常規(guī)的寫(xiě)法,必須保證寫(xiě)全元素,包括順序也得一樣。

> db.inventory.find({tags:["blank","red"]}){ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }

不寫(xiě)全了,查不到

> db.inventory.find({tags:["blank"]})

如果只想查詢包括 blank 的數(shù)據(jù),得這么寫(xiě)

> db.inventory.find({tags:"blank"}){ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }

順序不一樣,查不全數(shù)據(jù)

> db.inventory.find({tags:["red", "blank"]}){ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }>

那怎么辦,我就想查詢包括 blank 和 red 的都行的數(shù)據(jù),與順序無(wú)關(guān)的。這樣,加個(gè) $all

> db.inventory.find({tags:{$all:["red", "blank"]}}){ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }

查詢的至少一個(gè)元素值,dim_cm>20的數(shù)據(jù)

> db.inventory.find({dim_cm:{$gt:20}}){ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }> db.inventory.find({dim_cm:{$gt:21}}){ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }

但要查詢至少數(shù)組的一個(gè)元素值是,dim_cm>15 and dim_cm<20,這樣寫(xiě)是不行的

> db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } ){ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd2"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }

需要引入 $elemMath 運(yùn)算符

> db.inventory.find( { dim_cm: {$elemMatch: { $gt: 15, $lt: 20 }} } ){ "_id" : ObjectId("5e070270c01e4d7f78f96cd2"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }

那我只想查詢數(shù)組中的第一個(gè)元素 dim_cm>25 的數(shù)據(jù)呢

> db.inventory.find({"dim_cm.1": {$gt: 25}}){ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }

查詢指定數(shù)組長(zhǎng)度的數(shù)據(jù),比如我想得到tags的元素?cái)?shù)量為3的數(shù)據(jù)

> db.inventory.find({tags: {$size: 3}}){ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }

數(shù)組中對(duì)象的查詢

> db.inventory.insertMany( [...    { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },...    { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },...    { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },...    { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },...    { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }... ]);{    "acknowledged" : true,    "insertedIds" : [        ObjectId("5e070b34c01e4d7f78f96cd3"),        ObjectId("5e070b34c01e4d7f78f96cd4"),        ObjectId("5e070b34c01e4d7f78f96cd5"),        ObjectId("5e070b34c01e4d7f78f96cd6"),        ObjectId("5e070b34c01e4d7f78f96cd7")    ]}> db.inventory.find(){ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cc9"), "item" : "journal", "qty" : 25, "size" : { "h" : 14, "w" : 21, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96cca"), "item" : "notebook", "qty" : 50, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "A" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccb"), "item" : "paper", "qty" : 100, "size" : { "h" : 8.5, "w" : 11, "uom" : "in" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccc"), "item" : "planner", "qty" : 75, "size" : { "h" : 22.85, "w" : 30, "uom" : "cm" }, "status" : "D" }{ "_id" : ObjectId("5e06f1f5c01e4d7f78f96ccd"), "item" : "postcard", "qty" : 45, "size" : { "h" : 10, "w" : 15.25, "uom" : "cm" }, "status" : "A" }{ "_id" : ObjectId("5e070270c01e4d7f78f96cce"), "item" : "journal", "qty" : 25, "tags" : [ "blank", "red" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96ccf"), "item" : "notebook", "qty" : 50, "tags" : [ "red", "blank" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd0"), "item" : "paper", "qty" : 100, "tags" : [ "red", "blank", "plain" ], "dim_cm" : [ 14, 21 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd1"), "item" : "planner", "qty" : 75, "tags" : [ "blank", "red" ], "dim_cm" : [ 22.85, 30 ] }{ "_id" : ObjectId("5e070270c01e4d7f78f96cd2"), "item" : "postcard", "qty" : 45, "tags" : [ "blue" ], "dim_cm" : [ 10, 15.25 ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd4"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd5"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd6"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd7"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }

首先,常規(guī)的寫(xiě)法,元素的數(shù)量,順序都一致,否則查詢不到數(shù)據(jù)

> db.inventory.find({instock: {warehouse: "A",qty: 5}}){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }> db.inventory.find({instock: {qyt:5, warehouse: "A"}})>

我要查詢 instock 數(shù)組中,包含 qty,并且至少要有一個(gè)值是 qty <= 20的記錄

> db.inventory.find({"instock.qty": {$lte: 20}}){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd4"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd5"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd6"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd7"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }

當(dāng)然也可以指定數(shù)組的序號(hào),比如上面的查詢,我只想查詢到序號(hào)為0的

> db.inventory.find({"instock.0.qty": {$lte: 20}}){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd4"), "item" : "notebook", "instock" : [ { "warehouse" : "C", "qty" : 5 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd7"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }>

目前說(shuō)的都是一個(gè)元素的,如果是多個(gè)呢?還得用到$elemMatch

> db.inventory.find({"instock": {$elemMatch: {qty: {$gt: 10, $lte: 20}}}}){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd5"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd7"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }

比較一下下面這個(gè),只滿足一個(gè)條件即可,上面的則必須都滿足才行

> db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20 } } ){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd5"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd6"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd7"), "item" : "postcard", "instock" : [ { "warehouse" : "B", "qty" : 15 }, { "warehouse" : "C", "qty" : 35 } ] }

這樣也可以

> db.inventory.find({"instock": {$elemMatch: {qty: 5,warehouse: "A"}}}){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }

但這樣不行,也就是混合查詢中不能使用 $elemMatch

> db.inventory.find({"instock": {$elemMatch: {qty: {$lte: 20,$gt: 10},warehouse: "A"}}})

那怎么辦,這樣處理,拆開(kāi)

> db.inventory.find({"instock.qty": {$lte: 20,$gt: 10},"instock.warehouse": "A"}){ "_id" : ObjectId("5e070b34c01e4d7f78f96cd3"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd5"), "item" : "paper", "instock" : [ { "warehouse" : "A", "qty" : 60 }, { "warehouse" : "B", "qty" : 15 } ] }{ "_id" : ObjectId("5e070b34c01e4d7f78f96cd6"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }

最后

今天先這些了,數(shù)組與對(duì)象混合這個(gè),只能多練習(xí),要不然……

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
MongoDB(四):數(shù)據(jù)類(lèi)型、插入文檔、查詢文檔
外貿(mào)單據(jù)怎么寫(xiě)?
《MongoDB高手課》學(xué)習(xí)記錄(第二天)
UiPath中 數(shù)組、列表、字典的定義
SAP WM & Shipping integration: Copy WM Quantity
Xindice 簡(jiǎn)介 -開(kāi)放源碼 Native-XML 數(shù)據(jù)庫(kù)系統(tǒng) - java愛(ài)好者-希...
更多類(lèi)似文章 >>
生活服務(wù)
熱點(diǎn)新聞
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服