MongoDB
MongoDB
MongoDB is an open-source document database and leading NoSQL database. It is written in C++.it is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability.It works on concept of collection and document.
Syntax:
➤Basic syntax of use DATABASE statement is as follows −
.use DATABASE_NAME
>use music
➤Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.
>db.movie.insert({"name":" songdetails"})➤MongoDB db.dropDatabase() command is used to drop a existing database.
db.dropDatabase()
➤To check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
➤MongoDB db.createCollection(name, options) is used to create collection.
db.createCollection(name, options)
➤MongoDB's db.collection.drop() is used to drop a collection from the database.
➤MongoDB's db.collection.drop() is used to drop a collection from the database.
➤MongoDB's db.collection.update() is used to drop a collection from the database.
Syntax: >db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Example:
> db.songdetails.update({"Song Name":"Roja poonthooddam"},{$set:{"Song Name":"Enkee enathu kavithai"}})WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
➤ MongoDB's remove() method is used to remove a document from the collection.
here,I am going to remove thadsha
> db.students.remove({"Name":"thadsha"})
WriteResult({ "nRemoved" : 1 })
WriteResult({ "nRemoved" : 1 })
➤ Increase
If we want to increase a total by adding numbers to given we use $inc
Example:
>
db.studentmarks.update({"name":"Mala"},{$inc:{"maths_marks":6}})
➤gt is used as greater symbol
>
db.studentmarks.find({"maths_marks":{$gt:50}}).pretty()
It will list the names of student of maths marks greater the 50
➤Update is used to reset data
>db.studentmarks.update({"name":"lucky"},{$set:{"science_marks":75}})
It update the lucky's science marks to 75
➤and
>
db.studentmarks.find({$and:[{"maths_marks":{$gt:40}},{"science_marks":{$lt:40}}]}).pretty()
It give the data as both condition is true
or
>db.studentmarks.find({$or:[{"maths_marks":{$gt:40}},{"science_marks":{$lt:40}}]}).pretty()It give data as one condition is true
Rename
>
db.studentmarks.update({"name":"John"},{$rename:{"english_marks":"science_marks"}})
It rename the english_marks as science_marks



No comments: