What is MongoDB and its advantages and disadvantages? I will not introduce them here. Article The main purpose is to understand MongoDB through actual operations.
MongoDB installation and initialization
1: http://www.mongodb.org/
2 installation? Yes? It's super simple. Just decompress it !!!
3. directly set the bin folder under the installation directory to the environment variable for ease of use.
4. Creating a database file storage directory is actually creating a folder, for example, creating a new folder D: \ mongdb
5. Create a MongoDB log file. For example, create a new file D: \ mongdo \ logs \ MongoDB. log.
6. We can register MongoDB as a Windows Service so that we can use net start and net stop to start and close the service. Enter
Mongod -- dbpath = D: \ MongoDB -- logpath = D: \ MongoDB \ logs \ MongoDB. Log
7. Start the MongoDB Service
Net start MongoDB
Done !!!
Knife Test
Create a database named userinfo, create a users set (similar to a table in RDBMS), and insert two records (document in MongoDB ))
First, enter Mongo under CMD and enter the client command line. It is a JS interpreter that supports JavaScript syntax, such as for (VAR I = 0; I <10; I ++) DB. users. save ({Name: "name" + I, age: I}); and so on
1) create a userinfo Database
Use userinfo
Note: If the database userinfo does not exist, it will be automatically created for us. If there is, of course, it is directly switched to this database, for example:
2) create two records user1 and user2 and insert them to the users collection.
User1 = {Name: "zhangsan", age: 24 };
User2 = {Name: "Lisi", age: 23 };
DB. Users. Save (user1 );
DB. Users. Save (user2 );
Bytes ---------------------------------------------------------------------------------------------------------------------------
It can also be written in this way.
DB. Users. Save ({Name: "zhangsan", age: 24 });
DB. Users. Save ({Name: "Lisi", age: 23 });
Bytes ---------------------------------------------------------------------------------------------------------------------------
Note: we do not need to create the users set in advance here. We will automatically create the set when inserting data for the first time.
3) query the data inserted to the users set just now.
DB. Users. Find ();
Note:
Each document stored on mongdb will have a default primary key _ ID (if we do not display the specified one), as shown in. This _ id is automatically required by each document. Of course, its type does not have to be objectid, but the primary key must be unique.
Add, delete, modify, and add records for MongoDB
The article has already been introduced. One thing to note here is
Difference between DB. insert () and DB. Save:
Bytes -----------------------------------------------------------------------------------------------------------
Insert always inserts a new record.
Save insert if this record does not exist and update if yes
Bytes -----------------------------------------------------------------------------------------------------------
Insert some data for testing.
DB. Users. Save ({name: "Lucy", age: 23, Country: "Canada", Gender: 0 });
DB. Users. Save ({name: "Joe", age: 32, Country: "American", Gender: 1 });
DB. Users. Save ({name: "fanny", age: 22, Country: "China", Gender: 0 });
DB. Users. Save ({name: "Royall", age: 44, Country: "Africa", Gender: 1 });
DB. Users. Save ({name: "Michael", age: 25, Country: "China", Gender: 1 });
DB. Users. Save ({name: "Edgar", age: 24, Country: "China", Gender: 1 });
DB. Users. Save ({name: "apple", age: 39, Country: "Brazil", Gender: 0 });
DB. Users. Save ({name: "Bruce", age: 51, Country: "England", Gender: 1 });
DB. Users. Save ({name: "Lee", age: 12, Country: "Japan", Gender: 1 });
DB. Users. Save ({name: "Cang", age: 31, Country: "Japan", Gender: 0 });
DB. Users. Save ({name: "Hebe", age: 33, Country: "China", Gender: 0 });
DB. Users. Save ({name: "pig", age: 1, Country: "pighouse", Gender: 0 });
DB. Users. Save ({name: "Kevin", age: 48, Country: "China", Gender: 1 });
Modify record
Replace with a new document
DB. users. update ({"name": "Cang" },{ "name": "Hello", fav: {"favid": 100, "favname": "football "}})
Modify only one value
Change the country name of Bruce to Australia and age to 18.
DB. Users. Update ({"name": "Bruce" },{$ set: {"country": "Australia", "age": 18 }})
Delete key
DB. Users. Update ({"name": "Bruce" },{$ unset: {"country": 1 }})
The previous update operation only updates the first matching record. There are two parameters for update.
Update (query, OBJ, upsert, multi)
Upsert refers to insert if it does not exist
Multi is used to update all matched documents.
Delete record
Delete all records in the users set
DB. Users. Remove ()
Delete records with specified conditions
DB. Users. Remove ({"name": "Apple "})