Mongodb

Basic commands on mongo shell Image result for mongodb in ubuntu 16.04

Show all available databases:
 show dbs; 
Select a particular database to access, e.g. mydb. This will create mydb if it does not already exist:
 use mydb;
 Show all collections in the database (be sure to select one first, see above):
 show collections; 
Show all functions that can be used with the database: 
db.my db.help();
 To check your currently selected database,use the command db 
> db 
mydb
db.dropDatabase() command is us
ed to drop a existing database.
 db.dropDatabase()


insert collection 


We add tworecords to our collection test as:

>db.test.insert({"key":"value1","key2":"Val2","key3":"val3"})
 WriteResult({ "nInserted" : 1 })
> db.test.insert({"key":"value2","key2":"Val21","key3":"val31"})
 WriteResult({ "nInserted" : 1 })


 If we see them via find, they will look very ugly.
 > db.test.find()
 { "_id" : ObjectId("5790c5cecae25b3d38c3c7ae"), "key" : "value1", "key2" : "Val2 ", "key3" : "val3" }
{ "_id" : ObjectId("5790c5d9cae25b3d38c3c7af"), "key" : "value2", "key2" : "Val2 1", "key3" : "val31" }


To work around this and make them readable, use the pretty() function. 
> db.test.find().pretty()

{ "_id" : ObjectId("5790c5cecae25b3d38c3c7ae"),
 "key" : "value1",
 "key2" : "Val2",
 "key3" : "val3" }
 { "_id" : ObjectId("5790c5d9cae25b3d38c3c7af"),
 "key" : "value2",
 "key2" : "Val21",
 "key3" : "val31" }

 CRUD Operation
 Create 
db.people.insert({name: 'Tom', age: 28}); Or db.people.save({name: 'Tom', age: 28});

 Update
 Update the entire object:
 db.people.update({name: 'Tom'}, {age: 29, name: 'Tom'})

 Delete
 Deletes all documents matching the query parameter:
db.people.deleteMany({name: 'Tom'})


 Read 
Query for all the docs in the people collection that have a name field with a value of 'Tom': db.people.find({name: 'Tom'})




Comments

Popular posts from this blog

CSS

Bootstrap