Introduction to common Query operations in MongoDB (with code)

不言
Release: 2019-03-23 16:39:59
forward
3529 people have browsed it

This article brings you an introduction to the common Query operations of MongoDB (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Foreword: The visualization tool used is Studio 3T, official website-->https://studio3t.com/
Version number: MongoDB shell version v3.4.2
How to use: https:/ /blog.csdn.net/weixin_...
What to watch: focus on the operators.
How to search: Press ctrl F on this page and enter keywords to search

1. Commonly used Query
For the convenience of operation, delete all documents before inserting the original data (Please operate with caution in the project!):

db.getCollection("inventory").deleteMany({})
Copy after login

0. View all documents

db.getCollection("inventory").find({})
Copy after login

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 after login
Copy after login

1.2. Find documents where size.h is equal to 14, size.w is equal to 21, and size.uom is equal to cm

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

1.3. Find size.uom is equal to Documentation for in

db.inventory.find( { "size.uom": "in" } )
Copy after login

Note: When looking up individual object properties, be sure to include quotes!

1.4. Find and return the specified fields in the object

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

1.5. Find and filter the specified fields in the object

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

2. Array search
2.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 after login
Copy after login
Copy after login
Copy after login

2.2. Find documents with tags=["red", "blank"]

db.inventory.find( { tags: ["red", "blank"] } )
Copy after login

Note: It is not an inclusion relationship, that is tags: ["red", "blank", "plain"] are not included

2.3. Find documents whose tags contain red

db.inventory.find( { tags: "red" } )
Copy after login

Note: You cannot write db.inventory.find( { tags: ["red"] } ) like this, which means you are looking for documents whose tags are red

3. Search for objects contained in arrays
3.1. Original 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 after login
Copy after login
Copy after login

3.2. Find an object in the array that meets the conditions (not included). As long as there is an object in the array that meets the conditions, the entire array will be returned.

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

Must strictly follow the order of the fields. If the order of the fields is changed,cannot be found, as follows:

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

3.3. Find the element object in the array, there is one The qty=5 of the element object, or the warehouse=A

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

3.4. Find the object in the array and return a certain attribute of the object

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

4. Ordinary search
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 after login

4.2. Query and return specified fields
Under the condition of status=A, return _id, item, status fields

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

Result:

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

4.3. From 4.2, it can be seen that _id is automatically carried and can be removed, as follows
Query without (removed) id:

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

Note: In addition to the id that can be filtered out while retaining other fields, other fields cannot be 0 while also writing 1
For example:

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

will report an error

Introduction to common Query operations in MongoDB (with code)

4.4. Exclude specific fields and return other fields

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

5. Find null or Non-existent key
5.1. Original data

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

5.2. Find documents where item is null, or documents that do not contain item

db.inventory.find( { item: null } )
Copy after login

2. Operators
1、$lt less than less than
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 after login
Copy after login

1.2、Find the document collection with "size.h" less than 15

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

1.3. Use $lt with AND
Find documents where size.h is less than 15, size.uom is in, and status is D

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

2.$lte less than equal is less than or equal to
2.1, original 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 after login
Copy after login
Copy after login

2.2. Find documents with instock.qty less than or equal to 20, and return the entire array as long as one object in the array meets the conditions

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

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 after login
Copy after login
Copy after login
Copy after login

3.2、Find documents with dim_cm greater than 25

db.inventory.find( { dim_cm: { $gt: 25 } } )
Copy after login

Note: as long as it contains Arrays of elements greater than 25 are all qualified

3.3. Find documents whose dim_cm is greater than 15, or less than 20, or both greater than 15 and less than 20

db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )
Copy after login

3.4 , Find documents where dim_cm is both greater than 22 and less than 30 (it is to judge whether a certain element of the array is greater than 22 and less than 30, rather than judging all elements of the array)

db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )
Copy after login

3.5. According to the array position Search
Find documents where thesecondelement of dim_cm is greater than 25

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

4, $size Search according to the length of the array
Find tagsDocument with a length of 3

db.inventory.find( { "tags": { $size: 3 } } )
Copy after login

5, $gte is greater than or equal to
5.1, Original 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 after login
Copy after login
Copy after login

5.2, Find the first element of the array A collection of documents whose qty (object) is greater than or equal to 20

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

6. $elemMatch object attribute matching
6.1. Search in the array for qty=5, warehouse="A " object and return the document collection

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

6.2. Find the document collection in the array that matches qty greater than 10andless than or equal to 20

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

如果不使用 $elemMatch 的话,就表示 qty 大于 10或者小于等于 20,官方文档意思是,不在数组的某一个元素找 既满足条件 A 又满足条件 B 的 qty,而是在数组的所有元素上找,满足条件 A 或满足条件 B 的 qty

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

7、$slice 返回数组特定位置的元素
7.1、原数据

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 after login
Copy after login
Copy after login
Copy after login

7.2、查找并返回 tags 数组的最后一个元素

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

结果:

{ "_id" : ObjectId("5c91dce5e98d5972748780e6"), "item" : "journal", "tags" : [ "red" ] }
Copy after login

8、$type 返回指定类型的元素
8.1、原数据

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

8.2、返回 null 类型的数据

db.inventory.find( { item : { $type: 10 } } )
Copy after login

类型如下:

Introduction to common Query operations in MongoDB (with code)

详细文档请看:https://docs.mongodb.com/manu...

9、$exists 返回存在/不存在的键
查找不存在 item 键的数据

db.inventory.find( { item : { $exists: false } } )
Copy after login

10、$all 包含
10.1、原数据

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 after login
Copy after login
Copy after login
Copy after login

10.2、查找 tags 数组包含["red", "blank"] 的文档

db.inventory.find( { tags: { $all: ["red", "blank"] } } )
Copy after login

综上:
数组用的:$all$size$slice
对象用的:$elemMatch

Query查询的详细文档请看:https://docs.mongodb.com/manu...
Operator的详细文档请看:https://docs.mongodb.com/manu...

本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注PHP中文网的mongodb视频教程栏目!

The above is the detailed content of Introduction to common Query operations in MongoDB (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!