In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to use CRUD in mongoDB. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. In terms of storage, non-relational databases can be stored on a larger scale. For example, the databases used by Facebook are non-relational databases.
two。 It is also an advantage of this database to use it more smoothly, giving full play to the distributed characteristics.
When I looked at the official documents, it was deadly. I talked about several insertion methods alone, and my head was too big. Now I would like to summarize the characteristics of each insertion method.
Db.collection.insert ()
Db.collection.insert () inserts one or more documents into the collection. To insert a document, pass a document to the method; if you want to insert multiple documents, you can use this method.
For example
Db.users.insert ([{name: "bob", age: 42, status: "A",}, {name: "ahn", age: 22, status: "A",}, {name: "xi", age: 34, status: "D",}])
If the insert is successful, it will return
WriteResult ({"nInserted": 3})
If there is an exception, the following will be returned:
WriteResult ({nInserted: 3, "writeConcernError": {"code": 64, "errmsg": "waiting for replication timed out at shard-a"}})
When we want to insert a piece of data, the use of insert method is a waste of memory, this time, we have long used to insert a single syntax db.collection.insertOne () to insert a single document document to the collection to give a small column to illustrate.
Db.users.insertOne ({name: "sue", age: 19, status: "P"})
If there is a single, there must be multiple, so what is it like? The syntax is very similar, db.collection.insertMany () this syntax is no different from the above, right or not, of course it is wrong, you think, if the added data is an array nested in the array, the performance of the first two methods will be greatly reduced, affecting the performance of the database. To cut the nonsense, Liezi walked for a while:
Db.users.insertMany ([{_ id: 1, name: "sue", age: 19, type: 1, status: "P", favorites: {artist: "Picasso", food: "pizza"}, finished: [17,3], badges: ["blue", "black"], points: [{points: 85, bonus: 20}, {points: 85 Bonus: 10}}, {_ id: 2, name: "bob", age: 42, type: 1, status: "A", favorites: {artist: "Miro", food: "meringue"}, finished: [11,25], badges: ["green"], points: [{points: 85, bonus: 20}, {points: 64 Bonus: 12}]}, {_ id: 3, name: "ahn", age: 22, type: 2, status: "A", favorites: {artist: "Cassatt", food: "cake"}, finished: [6], badges: ["blue", "Picasso"], points: [{points: 81, bonus: 8}, {points: 55 " Bonus: 20}]}, {_ id: 4, name: "xi", age: 34, type: 2, status: "D", favorites: {artist: "Chagall", food: "chocolate"}, finished: [5,11], badges: ["Picasso", "black"], points: [{points: 53, bonus: 15}, {points: 51] Bonus: 15}]}, {_ id: 5, name: "xyz", age: 23, type: 2, status: "D", favorites: {artist: "Noguchi", food: "nougat"}, finished: [14,6], badges: ["orange"], points: [{points: 71, bonus: 20}]}, {_ id: 6 Name: "abc", age: 43, type: 1, status: "A", favorites: {food: "pizza", artist: "Picasso"}, finished: [18,12], badges: ["black", "blue"], points: [{points: 78, bonus: 8}, {points: 57, bonus: 7}]}])
Note: insertOne () and insertMany () are version 3.2 syntax.
Now that it has been added, we have to look for it, right? there are also a lot of small things in the search, and there are many self-defined queries.
1. Query all
Db.users.find ({}) is equivalent to db.users.find ()
2. Specify is equal to condition
An query filter document can use: an expression to specify an equal condition to select all containing fields and equal to a specific all documents:
The following example retrieves all documents with a status field value of "P" or "D" from the user collection:
Db.users.find ({status: {$in: ["P", "D"]}})
3. Specify AND condition
A composite query can specify conditions on multiple fields of a collection document. Implicitly, a logical AND conjunction joins the clauses of the composite query, causing the query to select documents in the collection that match all conditions.
The following example retrieves all documents in the users collection where status equals "A" ``* * and * *`` age is less than ($lt) 30:
Db.users.find ({status: "A", age: {$lt: 30}})
4. Specify OR condition
By using the $or operator, you can specify a document that matches at least one condition in a composite query selection set that uses logical OR conjunctions to connect clauses.
The following example retrieves all documents in the users collection with status` equal to "A" * * or * * age less than ($lt) 30:
Db.users.find ({$or: [{status: "A"}, {age: {$lt: 30}}]})
5. Specify AND and OR conditions (for more accurate query)
In the following example, the composite query document selects all documents in the collection where status`` equals "A" and either age is less than ($lt) 30 or type equals 1:
Db.users.find ({status: "A", $or: [{age: {$lt: 30}}, {type: 1}]})
6. Exact matching on embedded documents
Use {:} and "for the query document to match the document, to specify the exact equality condition that matches the entire embedded document. An embedded document on an equality condition match needs to specify an exact match that includes the order of the fields.
In the following example, the query matches all favorites fields in that order: an embedded document containing only the ``artist equal to "Picasso"`` and the food field equal to "pizza":
Db.users.find ({favorites: {artist: "Picasso", food: "pizza"}})
7. The field embedded in the document is equal to the match.
In the following example, the query uses dot notation to match all favorites fields that are embedded documents that contain a field equal to "Picasso" ``artist`` (and possibly other fields):
Db.users.find ({"favorites.artist": "Picasso"})
8. Queries on arrays
Take one parameter: $elemMatch (this parameter is an array of exact values)
The following example queries a document with at least one element greater than ($gt) 15 and less than ($lt) 20 in the finished array:
Db.users.find ({finished: {$elemMatch: {$gt: 15, $lt: 20})
9. Embed the document array
Use an array index to match fields in an embedded document
In the following example, the query uses the dot notation to match all documents whose dadges is the first array with the element "black":
Db.users.find ({'points.0.points': {$lte: 55}})
10. Do not specify array index matching fields
If you don't know where the document is indexed in the array, use a period (.) Concatenate the name of the field that contains the array with the name of the field of the embedded document.
The following example selects at least one embedded document in all points`` arrays that contains a field points whose value is less than or equal to ``55:
Db.users.find ({'points.points': {$lte: 55}})
11. Specify multiple query criteria for array documents
A single element satisfies the query condition
Use the $elemMatch operator to specify composite conditions for array elements to query documents for which at least one element in the array meets all the specified criteria.
The following example queries the points array for at least one document that contains an embedded document with a points less than or equal to 70 and a field bonus equal to 20:
Db.users.find ({points: {$elemMatch: {points: {$lte: 70}, bonus: 20}
12. The combination of elements meets the query conditions
The following example queries documents where the points array contains elements that satisfy the query criteria in some combination; for example, one element satisfies the condition that points is less than or equal to 70 and another element satisfies the condition that bonus is equal to 20, or an element satisfies both conditions:
Db.users.find ({"points.points": {$lte: 70}, "points.bonus": 20})
Then there is the update, which is similar to the insertion method, and the update can be seen as a kind of insertion.
In the words of an official document:
If db.collection.update (), db.collection.updateOne (), db.collection.updateMany (), or db.collection.replaceOne () contains upsert: true and no document matches the specified filter, then this action creates a new document and inserts it. If there are matching documents, this action modifies or replaces the matching single or multiple documents.
This explanation, in my opinion, is that the corresponding data will be created when there is no such data, after all, it is a special method of insertion.
1. Db.collection.updateOne (): modify a single data
The following example uses the db.collection.updateOne () method on the users collection to update the first document update operation that matches based on the filter condition that favorites.artist equals "Picasso":
Use the $set operator to update the value of the favorites.food field to "pie" and update the value of the type field to 3
Db.users.updateOne ({"favorites.artist": "Picasso"}, {$set: {"favorites.food": "pie", type: 3},})
2. The usage of db.collection.update () is similar to that of db.collection.updateOne (). To make a difference, we use the parameter {multi: true}, which will have this parameter in your modified data, indicating that the modification is complete.
Db.users.update ({"favorites.artist": "Pisanello"}, {$set: {"favorites.food": "pizza", type: 0,}}, {multi: true})
3. Db.collection.updateMany (), will this be considered to be a lot of modifications? of course, it can be understood this way, but I prefer to understand it as modifying multiple parameters.
The following example is for everyone to see clearly the use of {upsert: true}, it can clearly return your modified value
Db.inspectors.updateMany ({"Sector": {$gt: 4}, "inspector": "R. Coltrane"}, {$set: {"Patrolling": false}}, {upsert: true})
4. Another modification is to replace the document with db.collection.replaceOne.
The following example uses the db.collection.replaceOne () method for the users collection to replace the * * first * * document matched by filtering the condition name equal to "sue" with the new document:
Db.users.replaceOne ({name: "abc"}, {name: "amy", age: 34, type: 2, status: "P", favorites: {"artist": "Dali", food: "donuts"}})
Walking, masturbation deleted:
1. Delete all documents db.collection.remove ()
This method is straightforward, which is equivalent to deleting the delete () of the table structure in sql
Db.users.remove ({})
As an alternative, use db.collection.remove () to remove all documents from the users collection where the status field is equal to "A" using the following example:
Db.users.remove ({status: "P"})
2. Delete only one document that meets the criteria, db.collection.deleteOne ()
The following example uses db.collection.deleteOne () to delete the first document where the status field equals "A":
Db.users.deleteOne ({status: "D"})
3. Delete all documents in the collection db.collection.deleteMany ()
The following example removes all documents from the users collection using the db.collection.deleteMany () method:
Db.users.deleteMany ({}) the above is how CRUD is used in the mongoDB shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.