Basic use of mongoDB (II), use of mongodb
Connect to the mongoDB server for basic database operations./bin/mongo 127.0.0.1: 12345
View the current database> show dbsadmin (empty) local 0.078G
But change the database (it will be automatically created if it does not exist)> use jeromeswitched to db jerome
Delete database> db. dropDatabase () {"dropped": "jerome", "OK": 1}
Delete table
12345678910 |
> > show tables jerome_collection jerome_coolection system.indexes > db.jerome_collection.drop() true > Show tables # The current table is deleted. jerome_coolection system.indexes > |
Write (Set Data Writing, in JSON format)> db. jerome_collection.insert ({x: 1}) WriteResult ({"nInserted": 1}) (insert successful)
Query> show dbsadmin (empty) jerome 0.078 GBlocal 0.078 GB> show collectionsjerome_collectionsystem.indexes> db. jerome_collection.find () {"_ id": ObjectId ("556fd29a4e8b96c5ebc42e63"), "x": 1}> db. jerome_collection.find ({x: 1}) # You can specify the {"_ id": ObjectId ("556fd29a4e8b96c5ebc42e63"), "x": 1}
(_ Id is a global field and will not be repeated in the database)
Test: Insert a data record with the id of 1.
1234567891011 |
> db.jerome_collection.insert({x:3,_id:1}) WriteResult({ "nInserted" : 1 }) > db.jerome_collection.insert({x:2,_id:1}) WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: jerome.jerome_collection.$_id_ dup key: { : 1.0 }" } }) > |
If you insert the same id twice, an error is returned. The id cannot be repeated.
Insert multiple data entries and test limit.
12345678 |
> for (I = 3; I <100; I ++) db. jerome_collection.insert ({x: I}) # You can use js syntax WriteResult({ "nInserted" : 1 }) > db.jerome_collection. find (). Count () # Find The total number of items 99 > db.jerome_collection. find ().skip(3).limit(2). sort ({X: 1}) # skip the first three, take two, and sort by x { "_id" : ObjectId( "556ff5e8d7e60a53de941a74" ), "x" : 4 } { "_id" : ObjectId( "556ff5e8d7e60a53de941a75" ), "x" : 5 } > |
Updating Updata receives at least two parameters, one for searching and the other for updating data.
12345678 |
> db.jerome_collection. find ({x:1}) { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 1 } > db.jerome_collection.update({x:1},{x:999}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({X: 1}) # No more > db.jerome_collection. find ({x:999}) { "_id" : ObjectId( "556fd29a4e8b96c5ebc42e63" ), "x" : 999 } > |
Partial update operator (set)
1234567 |
> db.jerome_collection.insert({x:100,y:100,z:100}) WriteResult({ "nInserted" : 1 }) > db.jerome_collection.update({z:100},{$ set :{y:99}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({z:100}) { "_id" : ObjectId( "556ff84a1c99195ded71252e" ), "x" : 100, "y" : 99, "z" : 100 } > |
It is automatically created when no data is updated.
12345678910 |
> db.jerome_collection. find ({y:100}) > db.jerome_collection.update({y:100},{y:999}, true ) WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : ObjectId( "556ff9556db7cf8009b5edf8" ) }) > db.jerome_collection. find ({y:999}) { "_id" : ObjectId( "556ff9556db7cf8009b5edf8" ), "y" : 999 } |
Update multiple data entries
123456789101112131415161718192021 |
> for (I = 0; I <3; I ++) db. jerome_collection.insert ({c: 2}) # insert three WriteResult({ "nInserted" : 1 }) > db.jerome_collection. find ({c:2}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 2 } > Db. jerome_collection.update ({c: 2 },{ c: 3}) # update WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) > db.jerome_collection. find ({c:2}) { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 2 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 2 } > db.jerome_collection. find ({C: 3}) # Only One update is found to prevent misoperation { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } > db.jerome_collection.update({c:2},{$ set :{c:3}}, false , true ) # Update multiple items WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }) > db.jerome_collection. find ({c:2}) > db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } |
Delete (parameters required)
1234567891011121314 |
> db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } > Db. jerome_collection.remove () # unavailable 2015-06-04T00:15:34.444-0700 remove needs a query at src /mongo/shell/collection .js:299 > db.jerome_collection. find ({c:3}) { "_id" : ObjectId( "556ffa011c99195ded71252f" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712530" ), "c" : 3 } { "_id" : ObjectId( "556ffa011c99195ded712531" ), "c" : 3 } > Db. jerome_collection.remove ({c: 3}) # The deletion must have parameters. WriteResult({ "nRemoved" : 3 }) > db.jerome_collection. find ({C: 3}) # deleted successfully > |
When there is a large amount of index data, the indexing speed is faster. View set Indexes
1234567891011121314 |
> for (I = 0; I <100; I ++) db. jerome_collection.insert ({x: I}) # add Test Data WriteResult({ "nInserted" : 1 }) > db.jerome_collection.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_" , "ns" : "jerome.jerome_collection" } ] > |
There is only one default index.
Index 1 indicates positive sorting, and-1 indicates reverse sorting.
123456789101112131415161718192021222324252627 |
> db.jerome_collection.ensureIndex({x:1}) { "createdCollectionAutomatically" : false , "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.jerome_collection.getIndexes() [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_" , "ns" : "jerome.jerome_collection" }, { "v" : 1, "key" : { "x" : 1 }, "name" : "x_1" , "ns" : "jerome.jerome_collection" } ] > |
(It is better to create an index before using the database)
Although the index slows down the number of writes, the query speed becomes faster.