This document records how to update the elements in an array in MongoDB Collection. Suppose a record in collection has the following format:
Now to delete the scores array, "type" is "homework", the smaller one score. In, the smaller score is 54.759 ...
The update usage on MongoDB is as follows:
Db.collection.update (query, update, options)
Where query says: Updated condition, update means: What to update, Options: Update option (for example, insert when condition mismatch)
Here, our update condition is "_id" whether match; use $pull to delete the scores array, "type" is "homework", the smaller one score. The explanations on $pull are as follows:
removesfrom the existing array all instances of a value or values that match a specified condition.
For example, the following statement: Update "_id" to 1 and delete all vote that are greater than or equal to 6 in the votes array.
#_id1votes35677}
#_id1votes3}
Therefore, our update is implemented as follows:
New Document ("_id", Document.get ("_id")); // Update Condition // what you want to update New New Document ("type", "homework"). Append ("Score"new document ("$pull", update));
The entire complete code is implemented as follows:
Importcom.mongodb.MongoClient;ImportCom.mongodb.MongoClientURI;Importcom.mongodb.client.MongoCollection;ImportCom.mongodb.client.MongoCursor;Importcom.mongodb.client.MongoDatabase;Importorg.bson.Document;ImportOrg.bson.conversions.Bson;Importjava.util.List;/*** Created by Administrator on 2017/11/2.*/ Public classHomeworkscore { Public Static voidMain (string[] args) {mongoclient mongoclient=NewMongoclient (NewMongoclienturi ("Mongodb://localhost")); Mongodatabase Database= Mongoclient.getdatabase ("School"); Mongocollection<Document> collection = database.getcollection ("Students"); Mongocursor<Document> cursor =Collection.find (). iterator (); while(Cursor.hasnext ()) {Document Document=Cursor.next (); Document UpdateQuery=NewDocument ("_id", Document.get ("_id"));//Update ConditionList<document> scores = (list<document>) document.get ("scores"); DoubleHomework_score_low, Home_work_score_high; Home_work_score_high= Scores.get (2). GetDouble ("Score"); Homework_score_low= Scores.get (3). GetDouble ("Score"); if(Home_work_score_high <Homework_score_low) {Homework_score_low=Home_work_score_high; } //what you want to updateBson update =NewDocument ("scores",NewDocument ("type", "homework"). Append ("Score", Homework_score_low)); Collection.updateone (UpdateQuery,NewDocument ("$pull", update)); System.out.println (document); } cursor.close (); }}
View Code
After the update is complete, execute: Db.students.find (). Pretty (). The "type" for "homework" has only one score, such as:
Reference Links:
Update method Official Document
$pull Operator Official documentation
Here's a look at how to add an element to an empty array in collection. For example, an initial record is as follows: Comments is an array and now you want to add a document to the array
The $push operator in the update operation needs to be used: $push
the operator appends a specified value to an array. The first parameter of update () is the updating condition, and the second parameter is the update. A typical $push example is as follows:
Db.students.update ( {_id:1}, {$push: {scores:89}})
_ID the scores array in the record to 1, and then adds an element of 89.
New Document ("Author", name). Append ("Body", body). Append ("email"newnew Document ("Comments" , comment)));
The records after the update are:
For $push, refer to: $push
Summary: This article is the third chapter in MongoDB University M101 for Java developers homework. One important feature of MongoDB that distinguishes it from other relational databases is: Prejoin. When you need to federate multiple fields, MySQL needs a join, while MongoDB is Embedded all related fields in prejoin form in the pre-designed data store, thus eliminating the need for join operations when querying for data.
When you need to operate on the records in MongoDB Collection, more than Google, do not need to remember to update, delete and other operations. For example, Google:mongodb add element to array, you can find how to add an element to an array
Original: http://www.cnblogs.com/hapjin/p/7776595.html
MongoDB Update Array elements