MongoDB Shell version v3.4.2 How to use: blog.csdn.net/weixin_3999… What to watch for: Focus on the operators. How to find: Press CTRL +F on this page to enter the keyword to find

Delete all documents before inserting the original data for ease of operation (please be careful in this project!) :

db.getCollection("inventory").deleteMany({})
Copy the code

0. View all documents

db.getCollection("inventory").find({})
Copy the code

1. Object Search 1.1. Original 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"}]);Copy the code

Size. H = 14, size. W = 21, size

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

Size. Uom = in

db.inventory.find( { "size.uom": "in"})Copy the code

Note: When looking for individual object attributes, be sure to use quotes!

Find and return the specified field in the object

db.inventory.find(
   { status: "A" },
   { item: 1, status: 1, "size.uom": 1})Copy the code

1.5. Find and filter the specified fields in the object

db.inventory.find(
   { status: "A" },
   { "size.uom": 0})Copy the code

2. Array search 2.1. Raw data

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]}]);Copy the code

Find the document tags=[“red”, “blank”]

db.inventory.find( { tags: ["red"."blank"]})Copy the code

Note: Tags: [“red”, “blank”, “plain”] are not included

2.3. Find documents containing RED tags

db.inventory.find( { tags: "red"})Copy the code

Note: db.inventory.find({tags: [“red”]}) cannot be written to indicate that tags are red documents


3, array contains object lookup 3.1, raw data

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 } ] }
]);
Copy the code

If there is only one object in the array that meets the condition, return the entire array

db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )
Copy the code

Strictly follow the order of the fields, if you reverse the order of the fieldsCan't findThat is as follows:

db.inventory.find( { "instock": { qty: 5, warehouse: "A"}})Copy the code

Qty =5; warehouse=A; warehouse=A

db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A"})Copy the code

3.4. Find an object in an array and return an attribute of the object

db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1})Copy the code

4.1. Original data

db.inventory.insertMany( [
  { item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
  { item: "notebook", status: "A", size: {h: 8.5, w: 11, uom:"in" }, instock: [ { warehouse: "C", qty: 5 } ] },
  { item: "paper", status: "D", size: {h: 8.5, w: 11, uom:"in" }, instock: [ { warehouse: "A", qty: 60 } ] },
  { item: "planner", status: "D", size: {h: 22.85, w: 30, uom:"cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
  { item: "postcard", status: "A", size: {h: 10, w: 15.25, uom:"cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);
Copy the code

If status=A, return _id, item, status; if status=A, return _id, item, status

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
Copy the code

Results:

{ "_id" : ObjectId("5c91cd53e98d5972748780e1"), 
    "item" : "journal"."status" : "A"} / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - {"_id" : ObjectId("5c91cd53e98d5972748780e2"), 
    "item" : "notebook"."status" : "A"} / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - {"_id" : ObjectId("5c91cd53e98d5972748780e5"), 
    "item" : "postcard"."status" : "A"}
Copy the code

4.3 as can be seen from 4.2, id is automatically carried and can be removed. The following query does not carry id:

db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
Copy the code

Note: id can be filtered out and other fields can be reserved. Other fields cannot be written as 1 at the same time as 0.

db.inventory.find( { status: "A" }, { item: 1, status: 0 } )
Copy the code

complains

4.4 exclude specific fields and return other fields

db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
Copy the code

Search for null or nonexistent keys

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])
Copy the code

5.2 Find documents where item is null, or where item is not included

db.inventory.find( { item: null } )
Copy the code

$lt less than 1.1 $lt less than 1.1

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"}]);Copy the code

1.2, find the set of documents whose “sie.h” is less than 15

db.inventory.find( { "size.h": { $lt: 15}})Copy the code

Size. H < 15, size. Uom is in, AND status is D

db.inventory.find( { "size.h": { $lt: 15},"size.uom": "in", status: "D"})Copy the code

$lte less than equal

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 } ] }
]);
Copy the code

If instock.qty is less than or equal to 20, return the entire array as long as there is one object in the array

db.inventory.find( { 'instock.qty': { $lte: 20}})Copy the code

3, $gt greater than 3.1, original data

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]}]);Copy the code

3.2 Find the document whose DIM_CM is greater than 25

db.inventory.find( { dim_cm: { $gt: 25}})Copy the code

Note: Any array that contains more than 25 elements is eligible

Find all files whose DIM_CM is greater than 15, less than 20, or both

db.inventory.find( { dim_cm: { $gt: 15.$lt: 20}})Copy the code

3.4 Find all elements in array whose DIM_cm is greater than 22 and less than 30

db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22.$lt: 30}}})Copy the code

3.5. Find documents where the second element of DIM_CM is greater than 25 based on array position lookup

db.inventory.find( { "dim_cm.1": { $gt: 25}})Copy the code

$size = 1; $size = 1; $size = 1

db.inventory.find( { "tags": { $size: 3}})Copy the code

5. $GTE is greater than or equal to 5.1

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 } ] }
]);
Copy the code

Find a set of documents whose QTY is greater than or equal to 20 for the first element of the array

db.inventory.find( { 'instock.0.qty': { $gte: 20}})Copy the code

Qty =5, warehouse=”A”; $elemMatch = 6.1

db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A"}}})Copy the code

6.2 Find the set of documents in the array whose QTY is greater than 10 and less than or equal to 20

db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20}}}})Copy the code

If $elemMatch is not used, the QTY is greater than 10orLess than or equal to 20, the official document says, instead of looking for qTy that satisfies both condition A and condition B in one element of the array, look for QTY that satisfies condition A or condition B in all elements of the array

db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20}})Copy the code

$slice returns the element at a specific location in the array

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]}]);Copy the code

Find and return the last element of the tags array

db.inventory.find( { item: "journal" }, { item: 1, qty: 0, tags: { $slice: -1}})Copy the code

Results:

{ 
    "_id" : ObjectId("5c91dce5e98d5972748780e6"), 
    "item" : "journal"."tags" : [
        "red"]}Copy the code

$type returns element 8.1 of the specified type

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])
Copy the code

8.2 return null data

db.inventory.find( { item : { $type: 10}})Copy the code

The types are as follows:

Detailed documentation see: docs.mongodb.com/manual/refe…

$exists returns a key that does not exist

db.inventory.find( { item : { $exists: false}})Copy the code

$all contains 10.1, original data

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]}]);Copy the code

Find documents with tags array containing [“red”, “blank”]

db.inventory.find( { tags: { $all: ["red"."blank"]}})Copy the code

$elemMatch ($all, $size, $slice)

The detailed document please see Query Query: docs.mongodb.com/manual/tuto… The detailed document please see Operator: docs.mongodb.com/manual/refe…


(after)