Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB Note 4-query Operation

Shulou Source: shulou.com Published: 2022-06-01 17:25:36 09月15日 Update

1. Specify the return key 2, query condition 2.1 comparison operator $lt=

$nephrite =

2.2 query conditions query the names and ages of students aged 25 to 27 db.persons.find ({age: {$gte:25,$lte:27}}, {_ id:0,name:1,age:1}) query the math scores of all students who are not Korean students db.persons.find ({country: {$ne: "Korea"}}, {_ id:0,country:1) Db.persons.find ({country: {$in: ["China", "USA"]}}, {_ id:0,name:1,country:1}) query the name and country db.persons.find ({country: {$nin: ["China", "USA"]}}, {_ id:0,name:1) of students whose nationality is not Chinese or American. Country:1}) 2.4OR queries for information on students whose scores are greater than 85 in Chinese or 90 in English ({$or: [{c: {$gt:85}}, {e: {$gt:90}}]}, {_ id:0,name:1,c:1,e:1}) 2.5NULL finds out that students whose sex is null will first increase the gender of Chinese students by db.persons.update ({country: "China"}, {$set: {sex: "m"}). False,true) perform query operations db.persons.find ({sex: {$in: [null]}}, {_ id:0,country:1}) 2.6 regular query db.persons.find ({name:/zhangsan}, {_ id:0,name:1}) 2.7$ not is used to reverse db.person.find ({name: {$not:/zhang/}}, {_ id:0) Name:1}) 2.8. applications of $all and index query students who like "Mongodb" and "JS" db.persons.find ({books: {$all: ["MONGODB", "JS"]}}, {_ id:0,books:1,name:1}) query information about students whose second book is java db.persons.find ({"books.1": "JAVA"}, {_ id:0,name:1,books:1}) 2.9 query array $size of specified length It cannot be used with comparators. (drawbacks) db.persons.find ({books: {$size:5}}, {_ id:0,name:1}) 2.10 query the 2nd to 4th books on the jim shelf db.persons.find ({name: "jim"}, {books: {$slice: [1m3]}}) 2.11 query the last book db.persons.find ({name: "jim"}, {books: {$slice:-1}, _ id:0) Name:1}) 2.12 document query add educational background document to jim var jim = [{

School: "K"

Score: "A"

}, {

School: "L"

Score: "B"

}, {

School: "J"

Score: "A+"

}]

Db.persons.update ({name: "jim"}, {$set: {school:jim}}) query students who have studied in K db.persons.find ({"school.school": "K"}, {_ id:0,name:1}) db.persons.find ({school: {$elemMatch: {"school": "K"}}, {_ id:0,name:1}) 2.13$ where query age over 22 years old, like reading C++ books We can use $where for complex queries about students who have studied in K School, because he is omnipotent, but we should try to avoid using it less because it will have a performance cost.

Db.persons.find ({"$where": function () {

/ / every document that gets the query result

Var books = this.books

/ / get the school object in the document

Var school = this.school

/ / if age > = 22

If (this.age > 22) {

Var php = null

/ / traversing books

For (var I = 0; I < books.length; iTunes +) {

If (books[ I] = = "C++") {

Php = books [I]

/ / if the school is real

If (school) {

For (var j = 0; j < school.length; jacks +) {

/ / to judge whether to go to school in K or not.

If (school [j] .school = = "K") {

/ / the return is true

Return true

}

}

Break

}

}

}

}

})

Tags: Query student document information name grade China country nationality school age condition United States Category complexity bookshelf book price education object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno MariaDB macOS Huawei MySQL