MongoDB is traditional relational databases. There are no tables, rows or columns. MongoDB uses collections and objects. Think of collections as tables, and objects as table rows.There are some basic commands.
Basic Concept of MongoDB
Print a list of all collections for the current database.
In MongoDB collection is equal to a table and each record saves in a collection.The collection exists within the single database which is schema-less.There are two methods to create the collection in MongoDB using insert and create collection method.
Using create collection method:
if collection(Table) does not exist then first it creates and then it inserts data.A collection is automatically created in MongoDB while inserting data in a document.
Basic Concept of MongoDB
- Database holds a set of collections(Tables)
- Collection holds a set of documents(Rows)
- Document is a set of fields(Column)
- Field is a key-value pair(key=>value)
- Key is a name (string)
- Value is a - basic type like string, integer, float, etc.
show dbs
Use Database:use db_name
Display collection:Print a list of all collections for the current database.
show collections
Create Collection and Insert data:In MongoDB collection is equal to a table and each record saves in a collection.The collection exists within the single database which is schema-less.There are two methods to create the collection in MongoDB using insert and create collection method.
Using create collection method:
db.createCollection("teams");
db.teams.insert({"age":1})
Using Save Method:if collection(Table) does not exist then first it creates and then it inserts data.A collection is automatically created in MongoDB while inserting data in a document.
db.teams.save({country:"France",Rank:"A"})
Fetch Data:db.teams.find();
Specify Conditions with Operatorsdb.teams.find({"Rank":"B"}); db.teams.find({"salary":{ $gt: 30000 }});Logical AND
db.teams.find( { "salary": "200", "zipcode": "201301" } )Logical OR
db.teams.find(
{ $or: [ { "salary": "200" }, { "zipcode": "201301" } ] }
)
Update Collectiondb.people.update(
{condtion},
{
updated column
},
{ upsert: true }
)
db.people.update(
{ name: "Andy" },
{
name: "Andy",
rating: 1,
score: 1
},
{ upsert: true }
)
When you execute an update() with upsert: true and the query matches no existing document, MongoDB will refuse to insert a new document.
Comments
Post a Comment