Windows MongoDB Basic Gameplay series
- Introduction and installation of the Windows MongoDB Basic gameplay series
- Windows MONGODB Basic Gameplay series two curd operations (create, update, read and delete)
- Windows MONGODB Basic Gameplay Series two Curd additional one
Said in front of the words in the series two curd just simple walk a bit of code operation of the process, which is all simple basic operation of the content, after I carefully read the next content after the decision to enrich the previous operation, and a simple demo out of the actual combat, The demo-specific collocation language is temporarily determined between PHP and Nodejs. Add content to the list of things that are not finished with the document
- Create and return values for collections
- Ways to add multiple documents
Collection creation and return value ① when inserting a document into a collection, if the collection does not exist, adding content to the document automatically creates the collection. This is not the same as MySQL, MySQL must first create libraries, tables, and then add content to the table.
② this time is not practical operation, according to the previous operation I went again, this time to pay attention to a small detail. Adding content succeeds returns a Writeresult ({"ninserted": 1}), which represents the number of inserted bars.
Methods for adding multiple documents ① Create an array document and add the array document.
// storing multiple documents in an array var users = [{ "name": "A", "age": },{"name": "B", ' Age ':},{ "name": "C", "age": (+)]; // Adding array operations Db.testData.insert (users);
View Code② provides an API that can be used to add multiple documents after the 2.6 release, and the Operation process expansion code.
// Initialize Bulk () var bulk = Db.testData.initializeUnorderedBulkOp (); // unordered list Operations // var bulk = Db.collection.initializeOrderedBulkOp ();//Ordered list operation // insert to Bulk object Bulk.insert ({ "name": "A", "age": +}); Bulk.insert ({ "name": "B" , " Age ": +}); // Call the Execute () method of the Bulk object to add the document to the collection Bulk.execute ();
View CodeHere is just a description of the two methods, temporarily do not do in-depth study, feel very need to learn a little bit, follow-up tracking. Find out what's on the list of things you don't have to say
- Specify query and condition query use
- Method of embedded (Embedded) document query
- restricting queries
- Toggle cursor reading over and over again
- Performance issues with queries
Specify query and condition query using the previous learning has learned a simple query and conditional query, the comprehensive use is, for example, we want to find out the name is a, or the age is 33/34 of all people (casual conditions). At this point, I added two more times to the test before I added the data randomly.
Db.testData.find ({$or: [{' Name ': ' A '},{' age ': {$in: [33,34]}}});
View CodeMethod of embedded (Embedded) document query
//inserting analog dataDb.testData.insert ([{"Name": "Unofficial", "Age": 18, "Other":{ "Hobby": ["Eat", "drink", "play", "Happy"], "Speciality": ["Hands", "legs"] }, "Score": [58,59,60]},{ "Name": "Pushself", "Age": 100, "Other":{ "Hobby": ["Play"], "Speciality": ["Legs"] }, "Score": [60,70,80]}]);//The names of the students who found less than 60 of the language score were corresponding to the number of words .Db.testData.find ({"score.0": {$lt: 60}},{"_id": 0, "name": 1});//Find out the name of the good guy.Db.testData.find ({"Other.hobby": "Eat"},{"_id": 0, "name": 1});
View CodeLimit the query in Find (), in the second parameter to control the corresponding key value pair, for example, I do not need _id, just need to display the name, I can set the second parameter {"_id": 0, "name": 1}. Toggle cursor reading over and over again
// perform a Find operation var rs = db.testData.find ({}); // Traverse while (Rs.hasnext ()) { print (Tojson (Rs.next () )); // You can also use Printjson (Rs.next ());}
View CodeThe while can also use Mycursor.foreach (Printjson), instead of the performance problem of the operation query depends on the creation of the index, introducing a explain () method to reflect the query performance data.
//Insert a set of test dataDb.testData.insert ([{"_id": 1, "item": "F1", type: "Food", quantity:500 }, { "_id": 2, "item": "F2", type: "Food", quantity:100 }, { "_id": 3, "item": "P1", type: "Paper", quantity:200 }, { "_id": 4, "item": "P2", type: "Paper", quantity:150 }, { "_id": 5, "Item": "F3", type: "Food", quantity:300 }, { "_id": 6, "item": "T1", type: "Toys", quantity:500 }, { "_id": 7, "Item": "A1", type: "Apparel", quantity:250 }, { "_id": 8, "Item": "A2", type: "Apparel", quantity:400 }, { "_id": 9, "item": "T2", type: "Toys", quantity:50 }, { "_id": Ten, "Item": "F4", type: "Food", quantity:75 }]);//perform a Find operationDb.testData.find ({"Type": "Food"});//nscanned and Nscannedobjects show 10 that MongoDB had to scan ten documents (that is, all the documents in the collection) to find the four matching documents. //If you add an index to typeDb.testData.ensureIndex ({"Type": 1})//Querying four index documents and returning four document contents when performing query operations//performance of composite indexesDb.testData.ensureIndex ({quantity:1, type:1});d B.testdata.ensureindex ({type:1, Quantity:1 } );//obviously through the data can see the difference in performance, more of the later learning article to try to analyze the query optimization
View CodeDelete action for the previous content, here is only a little to add to the content is through the conditions to match the content, just delete a piece of content, the second parameter is set to 1. Db.testData.remove ({"Type": "Food"}, 1);
Windows MONGODB Basic Gameplay Series two Curd additional one