MongoDB CRUD - Odt

You might also like

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 3

Basics of MongoDB

[root@localhost ~]# sudo service mongod start


Redirecting to /bin/systemctl start mongod.service
[root@localhost ~]# mongod
[root@localhost ~]# mongo
MongoDB shell version: 2.4.6
connecting to: test

CRUD Operation in MongoDB


C Create:
> db.users.insert({"uid":1,"username": "joe", "age":27,"title":"MongoDB
Overview","email":"joe@gmail.com" })
> db.users.insert({"uid":2,"username": "smith", "age":20,"title":"MySQL
Overview","email":"smith@gmail.com" })
> db.users.insert({"uid":3,"username": "joe", "age":24,"title":"JAVA
Tutorial","email":"J123@gmail.com" })
> db.users.insert({"uid":4,"username": "Alice", "age":34,"title":"NOSQL
Overview","email":"Alice@gmail.com" })
> db.users.insert({"uid":5,"username": "Bob", "age":27,"title":"PL/SQL
Database","email":"bob@gmail.com" })
R Read:
> db.users.find()
{ "_id" : ObjectId("59b8fd92a553a0c013e92f86"), "uid" : 1, "username" : "joe", "age" : 27, "title" :
"MongoDB Overview", "email" : "joe@gmail.com" }
{ "_id" : ObjectId("59b8fda7a553a0c013e92f87"), "uid" : 2, "username" : "smith", "age" : 20, "title" :
"MySQL Overview", "email" : "smith@gmail.com" }
{ "_id" : ObjectId("59b8fdb2a553a0c013e92f88"), "uid" : 3, "username" : "joe", "age" : 24, "title" :
"JAVA Tutorial", "email" : "J123@gmail.com" }
{ "_id" : ObjectId("59b8fdbea553a0c013e92f89"), "uid" : 4, "username" : "Alice", "age" : 34, "title" :
"NOSQL Overview", "email" : "Alice@gmail.com" }
{ "_id" : ObjectId("59b8fdc6a553a0c013e92f8a"), "uid" : 5, "username" : "Bob", "age" : 27, "title" :
"PL/SQL Database", "email" : "bob@gmail.com" }
> db.users.findOne()
{
"_id" : ObjectId("59b8fd92a553a0c013e92f86"),
"uid" : 1,
"username" : "joe",
"age" : 27,
"title" : "MongoDB Overview",
"email" : "joe@gmail.com"
}
> db.users.find().pretty()
{
"_id" : ObjectId("59b8f89da50564eb2172a7c5"),
"uid" : 1,
"username" : "joe",
"age" : 27,
"title" : "MongoDB Overview",
"email" : "joe@gmail.com"
}
{
"_id" : ObjectId("59b8f8f4a50564eb2172a7c6"),
"uid" : 2,
"username" : "smith",
"age" : 30,
"title" : "MySQL Overview",
"email" : "smith@gmail.com"
}
{
"_id" : ObjectId("59b8f920a50564eb2172a7c7"),
"uid" : 3,
"username" : "joe",
"age" : 24,
"title" : "JAVA Tutorial",
"email" : "J123@gmail.com"
}
{
"_id" : ObjectId("59b8f948a50564eb2172a7c8"),
"uid" : 4,
"username" : "Alice",
"age" : 32,
"title" : "NOSQL Overview",
"email" : "Alice@gmail.com"
}
{
"_id" : ObjectId("59b8f971a50564eb2172a7c9"),
"uid" : 5,
"username" : "Bob",
"age" : 27,
"title" : "PL/SQL Database",
"email" : "bob@gmail.com"
}
U Update:
> db.users.update({"uid":4},{$set:{"title":"NOSQL Tutorial"}})
> db.users.find()
{ "_id" : ObjectId("59b8fd92a553a0c013e92f86"), "uid" : 1, "username" : "joe", "age" : 27, "title" :
"MongoDB Overview", "email" : "joe@gmail.com" }
{ "_id" : ObjectId("59b8fda7a553a0c013e92f87"), "uid" : 2, "username" : "smith", "age" : 20, "title" :
"MySQL Overview", "email" : "smith@gmail.com" }
{ "_id" : ObjectId("59b8fdb2a553a0c013e92f88"), "uid" : 3, "username" : "joe", "age" : 24, "title" :
"JAVA Tutorial", "email" : "J123@gmail.com" }
{ "_id" : ObjectId("59b8fdbea553a0c013e92f89"), "uid" : 4, "username" : "Alice", "age" : 34,
"title" : "NOSQL Tutorial", "email" : "Alice@gmail.com" }
{ "_id" : ObjectId("59b8fdc6a553a0c013e92f8a"), "uid" : 5, "username" : "Bob", "age" : 27, "title" :
"PL/SQL Database", "email" : "bob@gmail.com" }
D Delete :
> db.users.remove({"username":"Bob"})
> db.users.find()
{ "_id" : ObjectId("59b8fd92a553a0c013e92f86"), "uid" : 1, "username" : "joe", "age" : 27, "title" :
"MongoDB Overview", "email" : "joe@gmail.com" }
{ "_id" : ObjectId("59b8fda7a553a0c013e92f87"), "uid" : 2, "username" : "smith", "age" : 20, "title" :
"MySQL Overview", "email" : "smith@gmail.com" }
{ "_id" : ObjectId("59b8fdb2a553a0c013e92f88"), "uid" : 3, "username" : "joe", "age" : 24, "title" :
"JAVA Tutorial", "email" : "J123@gmail.com" }
{ "_id" : ObjectId("59b8fdbea553a0c013e92f89"), "uid" : 4, "username" : "Alice", "age" : 34, "title" :
"NOSQL Tutorial", "email" : "Alice@gmail.com" }
> db.users.remove()
> db.users.find()

You might also like