Learning record of mongodb master class (day 10)

Time:2020-2-25

Record of pits

Maybe there is a problem with the previous understanding. I looked at the official document yesterday. You can install it directly through brewMongoDB CommunityThe latest version of. Of course, there is no way to use the enterprise version. You have to download the compressed package and configure it yourself.

setup script

Environment needed

  1. 4.2Mongodb version only supports10.12Version above MacOS.
  2. Need to installxcode
  3. Of course, it needs to be installedHomebrew
  4. Need to installgcc
  5. Document address

setup script

#Register mongodb homebrew tap first
$> brew tap mongodb/brew

#The versions I can find now are as follows
$> brew search mongodb-community

==> Formulae
mongodb/brew/mongodb-community                           mongodb/brew/[email protected]                       mongodb/brew/[email protected]
mongodb/brew/mongodb-community-shell                     mongodb/brew/[email protected]                       mongodb/brew/[email protected]

#The default version without tag is 4.2.2
$> brew info mongodb-community

mongodb/brew/mongodb-community: stable 4.2.2
High-performance, schema-free, document-oriented database
https://www.mongodb.com/
Not installed
From: https://github.com/mongodb/homebrew-brew/blob/master/Formula/mongodb-community.rb
==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
Or, if you don't want/need a background service you can just run:
  mongod --config /usr/local/etc/mongod.conf
  
#Start installation
$> brew install mongodb-community

==> Caveats
To have launchd start mongodb/brew/mongodb-community now and restart at login:
  brew services start mongodb/brew/mongodb-community
Or, 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

#Start service
$> brew services start mongodb/brew/mongodb-community

==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community)

Installation location information

  • Program directory / usr / local / cell / mongodb community
  • Configuration file: / usr / local / etc / mongod.conf
  • System log / usr / local / var / log / mongodb
  • Data directory / usr / local / var / mongodb

Tenth days

Today’s review content is crud operation, directly in shell operation. All are examples of official websites.
Here’s the address: reference documents

Ready to practice data

> 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")
    ]
}

Query operation

Query all records

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" }

I found out, no{}Empty object, the result is the same.

Specify query criteria

SELECT * FROM inventory WHERE status = "D"
The corresponding MQL is as follows

> 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")
The corresponding MQL is as follows:

> 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
The corresponding MQL is as follows

> 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
The corresponding MQL is as follows

> 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%")
The corresponding MQL is as follows (the regular expression is used here)

> 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" }

Query embedded objects

For example, I need to find this record

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

I’m going to write that, which is to write the complete conditions

> 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" }

You can’t change the order. You have to write in order

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

You can’t write fewer fields

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

Keng dad, what’s going on
So, with the “.” operator

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

Add conditions

> 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" }

Write more complex points

> 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" }

Array query

> 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 ] }

It is the same problem as the query of objects in documents. To query a data, the general writing method must ensure that all elements are written, includingorderIt has to be the same.

> 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 ] }

I can’t find it if I don’t write it all

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

If you only want to query the data including blank, you have to write this

> 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 ] }

orderIt’s not the same. You can’t check all the data

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

What to do? I want to query the data including blank and red, which is not related to the order. So, add one$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 ] }

At least one element value of query, data with dim ﹤ cm > 20

> 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 ] }

But to query at least one element value of the array is, dim ﹤ cm > 15 and dim ﹤ cm < 20, so writing is not allowed

> 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 ] }

Need to introduce$elemMathoperator

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

So I just want to query the data of the first element in the array, dim cm > 25

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

Query the data with the specified array length, for example, I want to get the data with 3 elements of tags

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

Query of objects in array

> 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 } ] }

First of all, the general writing method, the number of elements, and the order are all the same, otherwise the data cannot be queried

> 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"}})
>

I want to query the instock array, which contains qty, and at least one record whose value is 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 } ] }

Of course, you can also specify the sequence number of the array. For example, in the above query, I just want to find the one with the sequence number of 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 } ] }
>

At present, it’s all about one element. What if it’s more than one? You need $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 } ] }

Compare the following one. Only one condition is satisfied. The above one must be satisfied

> 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 } ] }

It’s ok

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

But it doesn’t work, that is, $elemmatch can’t be used in mixed queries

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

What do you do? Deal with it like this. Take it apart

> 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 } ] }

Last

This is the first thing today. You can only practice mixing arrays and objects. Otherwise

Recommended Today

Installation of golang learning notes go1.15 (win / Linux / MacOS / docker installation)

Install go1.15 outline windows install golinux install gomacos install godocker install go summary Video address:https://www.bilibili.com/video/BV14a4y177XG?p=3 Windows install go 1. Click to download the MSI installation package of go1.15 64 bit systemhttps://studygolang.com/dl/golang/go1.15.windows-amd64.msi 32 bit systemhttps://studygolang.com/dl/golang/go1.15.windows-386.msi If the machine is not a master machine, it is usually no problem to choose 64 bit 2. Install and select […]