mongodb document operation - query
- mongodb document operation - query
-
- basic query
-
- prepare data
- 1. Query all employee information.
- 2. Query employees whose position is manager.
- 3. To query the employee information of MANAGER or CLERK, use `$in` and `$or` respectively.
- 4. Query the employees whose salary in department 10 is greater than 2000.
- 5. The query displays only two fields, the employee's name and department information.
- nested query
-
- prepare data
- 1. Query the information of students whose Chinese scores are 81 and English scores are 88.
- 2. Query the information of students with 75 points in Chinese, 69 points in English, and 80 points in mathematics.
- 3. Query the information of students whose Chinese scores are greater than 90.
- 4. Query the information of students whose English scores are greater than 88 points and mathematics scores are greater than or equal to 85 points.
- array query
- Array Nested Document Query
- Queries using operators
- Cursor usage
- Find distinct field values
- regular expression query
-
- 1. Query the information that the name field in the stu collection contains e characters, and the characters are not case-sensitive.
- 2. Query the information that the name field in the stu collection contains en characters, and the characters are not case-sensitive.
- 3. Query the document information in the stu collection whose name field contains e characters but ends with n characters, and the characters are not case-sensitive.
mongodb document operation - query
basic query
prepare data
db.emp.insert(
[
{
_id: 7369,
ename: 'SMITH',
job: 'CLERK',
mgr: 7902,
hiredate: '17-12-80',
sal: 800,
comm: 0,
deptno: 20
},
{
_id: 7499,
ename: 'ALLEN',
job: 'SALESMAN',
mgr: 7698,
hiredate: '20-02-81',
sal: 1600,
comm: 300,
deptno: 30
},
{
_id: 7521,
ename: 'WARD',
job: 'SALESMAN',
mgr: 7698,
hiredate: '22-02-81',
sal: 1250,
comm: 500,
deptno: 30
},
{
_id: 7566,
ename: 'JONES',
job: 'MANAGER',
mgr: 7839,
hiredate: '02-04-81',
sal: 2975,
comm: 0,
deptno: 20
},
{
_id: 7654,
ename: 'MARTIN',
job: 'SALESMAN',
mgr: 7698,
hiredate: '28-09-81',
sal: 1250,
comm: 1400,
deptno: 30
},
{
_id: 7698,
ename: 'BLAKE',
job: 'MANAGER',
mgr: 7839,
hiredate: '01-05-81',
sal: 2850,
comm: 0,
deptno: 30
},
{
_id: 7782,
ename: 'CLARK',
job: 'MANAGER',
mgr: 7839,
hiredate: '09-06-81',
sal: 2450,
comm: 0,
deptno: 10
},
{
_id: 7788,
ename: 'SCOTT',
job: 'ANALYST',
mgr: 7566,
hiredate: '19-04-87',
sal: 3000,
comm: 0,
deptno: 20
},
{
_id: 7839,
ename: 'KING',
job: 'PRESIDENT',
mgr: 0,
hiredate: '17-11-81',
sal: 5000,
comm: 0,
deptno: 10
},
{
_id: 7844,
ename: 'TURNER',
job: 'SALESMAN',
mgr: 7698,
hiredate: '08-09-81',
sal: 1500,
comm: 0,
deptno: 30
},
{
_id: 7876,
ename: 'ADAMS',
job: 'CLERK',
mgr: 7788,
hiredate: '23-05-87',
sal: 1100,
comm: 0,
deptno: 20
},
{
_id: 7900,
ename: 'JAMES',
job: 'CLERK',
mgr: 7698,
hiredate: '03-12-81',
sal: 950,
comm: 0,
deptno: 30
},
{
_id: 7902,
ename: 'FORD',
job: 'ANALYST',
mgr: 7566,
hiredate: '03-12-81',
sal: 3000,
comm: 0,
deptno: 20
},
{
_id: 7934,
ename: 'MILLER',
job: 'CLERK',
mgr: 7782,
hiredate: '23-01-82',
sal: 1300,
comm: 0,
deptno: 10
}
]
);
1. Query all employee information.
db.emp.find();
2. Query employees whose position is manager.
db.emp.find({
'job':'MANAGER'});
3. To query the employee information of MANAGER or CLERK, use $in
and $or
complete respectively.
- in:
field:{$in:[condition1,...]}
- or:
$or:[{field:condition1},...]
db.emp.find(
{
'job': {
$in: ['MANAGER', 'CLERK']
}
}
);
db.emp.find({
$or: [{
'job': 'MANAGER'
}, {
'job': 'CLERK'
}]
})
4. Query the employees whose salary in department 10 is greater than 2000.
- $gt: greater than
- $lt: less than
- $eq: equal to
- $ne: not equal to
db.emp.find({
'sal': {
$gt: 2000
}
})
5. The query displays only two fields, the employee's name and department information.
db.emp.find({
},{
'ename':1,'deptno':1,'_id':0})
nested query
prepare data
db.stu.insertMany([
{
_id: "stu0001",
name: "Mary",
age: 21,
grade: {
chinese: 80,
math: 85,
english: 90
},
books: ["Hadoop", "Java", "NoSQL"]
},
{
_id: "stu0002",
name: "Tom",
age: 20,
grade: {
chinese: 86,
math: 82,
english: 95
},
books: ["C++", "Java", "Oracle"]
},
{
_id: "stu0003",
name: "Mike",
age: 22,
grade: {
chinese: 81,
math: 90,
english: 88
},
books: ["Java", "MySQL", "PHP"]
},
{
_id: "stu0004",
name: "Jerry",
age: 20,
grade: {
chinese: 95,
math: 87,
english: 89
},
books: ["Hadoop", "Spark", "Java"]
},
{
_id: "stu0005",
name: "Evan",
age: 19,
grade: {
chinese: 75,
math: 80,
english: 69
},
books: ["C", "Python"]
},
{
_id: "stu0006",
name: "Oven",
age: 19,
grade: {
chinese: 79,
math: 90,
english: 99
},
books: ["C++", "Python"]
},
{
_id: "stu0007",
name: "Lily",
age: 21,
grade: {
chinese: 70,
math: 60,
english: 69
},
books: ["Java", "Spark", "Python"]
},
{
_id: "stu0008",
name: "Kelly",
age: 22,
grade: {
chinese: 70,
math: 64,
english: 67
},
books: ["C", "Spark", "Python"]
}
])
1. Query the information of students whose Chinese scores are 81 and English scores are 88.
db.stu.find(
{
'grade.chinese': 81,
'grade.english': 88
}
)
2. Query the information of students with 75 points in Chinese, 69 points in English, and 80 points in mathematics.
db.stu.find(
{
grade: {
chinese: 75,
math: 80,
english: 69
}
}
)
3. Query the information of students whose Chinese scores are greater than 90.
db.stu.find(
{
'grade.chinese':{
$gt:90}
}
)
4. Query the information of students whose English scores are greater than 88 points and mathematics scores are greater than or equal to 85 points.
db.stu.find(
{
'grade.english':{
$gt:88},
'grade.math':{
$gt:85}
}
)
array query
1. Query all student information in the stu collection with Hadoop, Java and Nosql books.
db.stu.find(
{
books: {
$all: ["Hadoop", "NoSQL", "Java"]
}
}
)
2. Query the information of all students who have Python and C books in the stu collection.
Using $all is equivalent to inclusion, not exact matching
db.stu.find(
{
books: {
$all: ["C", "Python"]
}
}
)
Array Nested Document Query
prepare data
var updateBooks = [
[{
"bookname": "Hadoop",
quantity: 2
}, {
"bookname": "Java",
quantity: 3
}, {
"bookname": "NoSQL",
quantity: 4
}],
[{
"bookname": "C++",
quantity: 4
}, {
"bookname": "Java",
quantity: 3
}, {
"bookname": "Oracle",
quantity: 5
}],
[{
"bookname": "Java",
quantity: 4
}, {
"bookname": "MySQL",
quantity: 1
}, {
"bookname": "PHP",
quantity: 1
}],
[{
"bookname": "Hadoop",
quantity: 3
}, {
"bookname": "Spark",
quantity: 2
}, {
"bookname": "Java",
quantity: 4
}],
[{
"bookname": "C",
quantity: 1
}, {
"bookname": "Python",
quantity: 1
}],
[{
"bookname": "Hadoop",
quantity: 1
}, {
"bookname": "MySQL",
quantity: 2
}],
[{
"bookname": "Oracle",
quantity: 3
}, {
"bookname": "Python",
quantity: 3
}],
[{
"bookname": "MySQL",
quantity: 2
}, {
"bookname": "Python",
quantity: 2
}]
]
for (let i = 1; i <= 8; i++) {
db.stu.update(
{
_id: 'stu000' + i,
},
{
$set: {
books: updateBooks[i - 1]
}
}
)
}
1. Query the information of students who have 4 books of Java.
db.stu.find(
{
books:{
bookname:'Java',
quantity:4
}
}
)
2. Query the student information whose first element in the array is greater than 3 books.
db.stu.find(
{
'books.0.quantity':{
$gt:3
}
}
)
3. At least one quantity value in the query document is greater than 4
$elemMatch means to traverse each element
db.stu.find(
{
books: {
$elemMatch:{
quantity:{
$gt:4}
}
}
}
)
Queries using operators
Insert two records.
db.stu.insertMany([
{
_id:"stu0009",name:"Kim",age:null },
{
_id:"stu0010",name:"Max"}
])
1. Query documents whose age value is null, including documents whose age value is missing.
db.stu.find(
{
age: {
$in: [
null,
''
]
}
}
)
2. Only return documents whose age value is null.
$type:10 means null
db.stu.find({
age: {
$type: 10
}
})
db.stu.find(
{
age: {
$in: [null],
$exists: true
}
}
)
3. Check the document for the missing age field.
db.stu.find(
{
age: {
$exists: false
}
}
)
4. Query the document information whose Chinese scores are less than English scores.
db.stu.find(
{
$expr: {
$lt: [
'$grade.chinese',
'$grade.english'
]
}
}
)
Cursor usage
You can think of a cursor as an iterator
1. Define all the student information of the query stu collection as the cursor mycursor;
var mycursor = db.stu.find()
2. Use the cursor mycursor to access each document in the stu collection and print it;
while(mycursor.hasNext()){
printjson(mycursor.next())
}
mycursor.forEach(function (k){
printjson(k)
})
3. Convert the cursor mycursor into an array, and query the third document in the array and output it;
var mycursor = db.stu.find()
printjson(mycursor[2])
- Use skip and limit to realize the pagination query of the stu collection, displaying 4 documents per page.
var page = 1
var mycursor = db.stu.find().limit(4).skip(page * 4 + 1)
while(mycursor.hasNext()){
printjson(mycursor.next())
}
Find distinct field values
Query which jobs are in the emp collection.
db.emp.distinct('job')
regular expression query
1. Query the information that the name field in the stu collection contains e characters, and the characters are not case-sensitive.
db.stu.find(
{
name: {
$regex:"e",
$options: "$i"
}
}
)
2. Query the information that the name field in the stu collection contains en characters, and the characters are not case-sensitive.
db.stu.find({
name: {
$regex: 'en',
$options: "$i"
}
})
3. Query the document information in the stu collection whose name field contains e characters but ends with n characters, and the characters are not case-sensitive.
db.stu.find({
name: {
$regex: /e.*n$/
}})