/*Create a User Database Named PersonDB */
MongoDB Enterprise > use personDB
switched to db personDB
/*Check you are in which database */
MongoDB Enterprise > db
personDB
/*Create a Post Variable */
MongoDB Enterprise > post={"name":"Rakesh","friend":477,"enemy":2}
{ "name" : "Rakesh", "friend" : 477, "enemy" : 2 }
/* Create Person table and Insert data into that table using post variable*/
MongoDB Enterprise > db.Person.insert(post)
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise > db.person.read()
MongoDB Enterprise > db.person.find()
-- Value did not return because MongoDB is highly case sensitive
/* search record inserted or not */
MongoDB Enterprise > db.Person.find()
{ "_id" : ObjectId("5896e738afd337d2cb8debce"), "name" : "Rakesh", "friend" : 477, "enemy" : 2 }
/* Now we need to add Address column as a embedded document using post variable */
-- The First step is to modify the variable post and add a "comment" key:
MongoDB Enterprise > post.address={"street":"L B S Nagar","House No":203,"Apartment":"Cansa Park"}
MongoDB Enterprise > db.Person.update({"name":"Rakesh"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.Person.find()
{ "_id" : ObjectId("5896e738afd337d2cb8debce"), "name" : "Rakesh", "friend" : 477, "enemy" : 2, "address" : { "street" : "L B S XXXX", "House No" : 1234, "Apartment" : "Salnsa XXX" } }
MongoDB Enterprise > db.Person.findone()
2017-02-05T14:44:09.204+0530 E QUERY [thread1] TypeError: db.Person.findone is not a function :
@(shell):1:1
MongoDB Enterprise > db.Person.findOne()
{
"_id" : ObjectId("5896e738afd337d2cb8debce"),
"name" : "Rakesh",
"friend" : 477,
"enemy" : 2,
"address" : {
"street" : "L B S XXXX",
"House No" : 1234,
"Apartment" : "Salnsa XXX"
}
}
/* Now we have to add a key Pin in address to Address key */
MongoDB Enterprise > post.address={"street":"L B S XXXX","House No":1234,"Apartment":"Salnsa XXX","pin":560017}
{
"street" : "L B S XXXX",
"House No" : 1234,
"Apartment" : "Salnsa XXX",
"pin" : 560017
}
MongoDB Enterprise > db.Person.update({"name":"Rakesh"},post)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
MongoDB Enterprise > db.Person.findOne()
{
"_id" : ObjectId("5896e738afd337d2cb8debce"),
"name" : "Rakesh",
"friend" : 477,
"enemy" : 2,
"address" : {
"street" : "L B S XXXX",
"House No" : 1234,
"Apartment" : "Salnsa XXX",
"pin" : 560017
}
}
Removing Documents
Now that there is a data on our database, let's delete it:
MongoDB Enterprise >
MongoDB Enterprise > db.Person.remove()
2017-02-05T15:36:30.004+0530 E QUERY [thread1] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:409:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:434:18
@(shell):1:1
MongoDB Enterprise > db.Person.remove({"name":"Rakesh"})
WriteResult({ "nRemoved" : 1 })
--This does not actually remove the collections, and any meta information about it will still exist.
MongoDB Enterprise > show collections;
Person
MongoDB Enterprise >