MongoDB is a document database, there are some specialized terms, similar to relational db, but there are differences, for example, collection similar to the relational DB table,document similar to the row,key/value pair similar to column. Document is using {} as a boundary, a Key/value pair using ":" Split, Key/value pair use "," split, for example
user={name: "Sue"}
In MongoDB, it is possible to define an array of document, which is useful for bulk updates and BULK INSERT operations.
userarray=[{name:"Sue", Age:24 }, {name:"Joe", Age:25 }, {name:"Pei", age:32 }]
MongoDB has a test db that can be switched to test DB using the use command when learning MongoDB.
Use test
One, insert operation
The insert operation for MongoDB is to insert the document into the collection, MongoDB provides three kinds of insert functions Db.collection.insert,db.collection.insertone and Db.collection.insertmany,insert functions can be inserted into a single doc, or an array of doc can be inserted.
1, insert a single document
user={name:"Test1", Age:}db.users.insert (user)
Db.users.insert ({name:"Test1", Age: $) Db.users.insertOne ({name:"Test1", Age: $)
2, BULK INSERT Document
user1={name:' T1 ', Age:}user2={name:' T2 ', age:}user3= {name:"T3", Age:}db.users.insert ([User1,user2,user3]) Db.users.insertMany ([User1,user2, USER3])--or Userarray=[User1,user2,user3]db.users.insertmany ([User1,user2,user3]) Db.users.insert ([User1,user2,user3])
Second, find operations
Syntax for query queries
Db.collection.find (<query filter>, <projection>)
Query filter is the filter for queries, projection is to look for specific key/value pair from document, similar to the WHERE clause and select clause of the relational db.
When no query filter is added, all document
Db.users.find () Db.users.find ({})
1,query Filter
1.1 In query filter, "=" uses key/value pair, or $eq indicates that the two are equivalent
{field: <value><field>: {$eq: <value>}}
For example, querying all the user age=21
Db.users.find ({age:21}) db.users.find ({age:{$eq:21}})
Inequalities using $ne representations
{field: {$ne: Value}}
For example, view all the user age<>21
Db.users.find ({age:{$ne: 21}})
1.2 ">", ">=", "<" "<=", respectively, using $GT, $gte, $lt and $lte representations, the format is
{field: {$lt: Value}} {field: {$gt: Value}} {field: {$lte: Value}} {field: {$gte: Value}}
For example, querying age<22 separately, age>22,age<=22,age>=22 all user
Db.users.find ({age:{$lt: ()}) Db.users.find ({age:{$gt:()})Db.users.find ({age:{$lte:]}) Db.users.find ({age:{$gte:22}})
1.3 The logical OR operator is represented using $or, and the format is
{$or: [{<expression1>}, {<expression2>}, ..., {<expressionN>}]}
For example, querying all the user age=21 or age=22
Db.users.find ({$or: [{age:21},{age:22}]})
1.4, the logic and operator use ",", or $and, the format is
{$and: [{<expression1>}, {<expression2>}, ..., {<expressionN>}]}
For example, the query name is "T1" and all the user age=21
Db.users.find ({$and: [{name: "T1"},{age:21}]}) Db.users.find ({name:"T1", age:21})
1.5, in the scope of the query, using $in representation, not in the range, using $nin representation, the format is
{field: {$in: [<value1>, <value2>,. ... <valueN> ]}} {field: {$nin: [<value1> ;, <value2>,. ... <valueN>]}}
For example, Query age is 21 or 22 of all the user, query age is not 21 and 22 of all user
Db.users.find ({age:{$in: [21,22]}}) Db.users.find ({age:{$nin: [21,22]}})
1.6 Other operators, please refer to the Official document "Query and Projection Operators"
2,projection
Projection is which field in the specified collection needs to be returned, and all filed are returned by default. If you do not specify a projection for "_id", then the result set returns "_id", and the details refer to the "Project fields to Return from Query"
A query projection to specifies which fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.
{field1: <value>, Field2: <value> ...}
Example:
Db.users.find ({$and: [{name: "T1"},{age:21}]},{name:1,_id:0}) Db.users.find ({$and: [{name:"T1"},{age:21}] },{name:0,_id:0})
Reference Documentation:
Query and Projection Operators
MongoDB CRUD Operations
MongoDB Query and insert operations