Getting started with MongoDb
After downloading mongodb, add "directory \ mongodb \ bin" to the environment variable"
Open the cmd window
-- Enable the mongodb Service
Mongod-dbpath "D: \ mongodb \ db"
-- Register a service
Mongod-dbpath "D: \ mongodb \ db"-logpath "D: \ mongodb \ log \ mongodb. log"-install-serviceName "MongoDB"
-- Start the service
Net start MongoDB;
-- View help
Db. help ();
-- Create a database (Database Name: zyh)
Use zyh;
-- View the set in the current database
Show collections;
Mongodb has no tables but a set.
-- Create a set
1. Add data directly to the set. If the set does not exist, it is automatically created (Set Name: yc)
Db. yc. insert ({"_ id": 1001, "name": "yc "});
In this case, you can use the show collections command to find two collections:
System. indexes; index set
Yc;
2. db. createCollection ("navy ");
-- Delete a set
Db. yc. drop ();
-- Delete a record (document)
Db. collection_name.remove ({condition });
Db. yc. remove ({"_ id": {"$ ne": 1001}); // $ ne: not equal to $ lt: less than $ gt: greater than $ lte: less than or equal to $ gte: greater than or equal
-- Add multiple records to the set
Db. yc. insert ([{"_ id": 1002, "name": "", "sex": "male" },{ "_ id": 1003, "name ": "Dangdang", "sex": "female"}]);
-- View data in the Set (. limit is pagination)
Db. yc. find ();
Data Type:
Null: {"x": null}
Boolean: {"x": true}
Value: {"x": 3.14} {"x": 3} NumberInt ("3") NumberLong ("3 ")
String: {"x": "hello "}
Date: {"x": new Date ()}
Regular Expression: {"x":/hello/ig}
Array: {"x": [1, 2, 3]}
Embedded document: {"x": {"foo": {bar }}}
Object id: {"x": ObjectId ()}
Binary:
Code: {"x": function (){}}
-- If yes, it is modified. If no, it is added.
Db. yc. save ({"_ id": 1004, "name": "navy1 "});
-- Modify
Db. yc. update ({condition}, {data to be modified });
Db. yc. update ({"_ id": 1002 },{ "name": "scott1", "sex": "F "})
Db. yc. insert ({"_ id": 1001, "url": "www.hyycinfo.com", "pageViews": 1 });
Db. yc. insert ({"_ id": 1002, "company": "yc", "url": "www.hyycinfo.com", "pageViews": 1 });
-- Modifier
$ Inc Add the corresponding value
Db. yc. update ({"_ id": 1001 },{ "$ inc": {"pageViews": 1 }}); -- increase the value of the pageViews key in the document with _ id 1001 by 1.
$ Set
Db. yc. update ({"_ id": 1002 },{ "name": "scott1", "sex": "F "})
Db. yc. update ({"_ id": 1002 },{ "$ set": {"name": "scott1", "sex": "F "}})
-- Convert company into an array
Db. yc. update ({"_ id": 1002 },{ "$ set": {"company": ["yc", "nh", "navy"]})
-- Delete key
Db. yc. update ({"_ id": 1002 },{ "$ unset": {"company": 1}) -- delete the company key in record 1002
-- Array Modifier
$ Push adds a value to the array. duplicate values may appear.
Db. yc. update ({"_ id": 1002 },{ "$ push": {"company": "SC "}});
$ Each
Db. yc. update ({"_ id": 1002 },{ "$ push": {"company": {"$ each": ["hg", "rc ", "jm"] }});
$ Slice specifies the maximum length. Its value must be negative, indicating that the last n values are retained.
Db. yc. update ({"_ id": 1002 },{ "$ push": {"company": {"$ each": ["yc1", "yc2 ", "yc"], "$ slice":-10 }}});
$ Pop deletes an element key from the array: 1 starting from the end of the data key:-1 starting from the header
Db. yc. update ({"_ id": 1002 },{ "$ pop": {"company": 1 }})
$ Pull: delete matched values from the array
Db. yc. update ({"_ id": 1002 },{ "$ pull": {"company": "SC "}})
Db. yc. insert ({
"_ Id": 1005,
"Content": "How are you going to eat today? ",
"Comments ":[
{"Comment": "Good", "count": 0 },
{"Comment": "Good", "count": 0 },
{"Comment": "very good", "count": 0}
]
})
-- Access through array subscript
Db. yc. update ({"_ id": 1004 },{ "$ inc": {"comments.1.count": 1 }});
Db. yc. update ({"comments. comment": "Good" },{ "$ inc": {"comments. $. count": 1 }})
Db. yc. update ({"comments. comment": "Good"}, {"$ set": {"comments. $. comment": "very good "}})
-- MongoDB only modifies one document at a time by default. To modify all records that meet the conditions, add the condition {multi: true}
Db. yc. update ({"comments. comment ":" Good "}, {" $ inc ": {" comments. $. count ": 1 }}, {multi: true })
Db. yc. update ({"comments. comment ":" Good "}, {" $ inc ": {" comments. $. count ": 1 }}, false, true)
-- Delete
Db. yc. remove ({condition });
-- Query
Db. yc. find (); -- Query all records
Db. yc. findOne (); -- query the first record
Db. yc. find ({"_ id": 1001}); -- where key = val
Db. yc. find ({"_ id": {"$ gt": 1001}); -- where key> val
-- $ In indicates that it is within a value.
Db. yc. find ({"_ id": {"$ in": [1003,]});
-- $ Or
Db. yc. find ({"$ or": [{"_ id": 1001 },{ "name": "zyh"}]});
-- And or
Db. yc. find ({"sex": "male", "$ or": [{"_ id": 1001 },{ "name": "zyh"}]});
--------------------------------------------------------------------------
For (I = 0; I <10; I ++ ){
Db. yc. insert ({"x": I });
}
Var cursor = db. yc. find ();
Var obj:
While (cursor. hasNext ()){
Obj = cursor. next ();
Print (obj );
}
Var cursor = db. yc. find ();
Cursor. forEach (function (x ){
Print (x );
});
-- Paging Query
Db. yc. find (). limit (3); -- the first three
Db. yc. find (). limit (3). skip (3); -- skip the first three items and check the next three items.
-- Sort
Db. yc. find (). sort ({"_ id":-1}); -- 1 is ascending-1 is descending
Db. yc. find (). sort ({"_ id":-1, "name": 1 });
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.