Question: What is Relationships in MongoDb?
Relationships in MongoDb represent how various documents are related to each other.
For Example:
There are various documents like user, address, payment details etc.
Connection between these documents know as Relationships.
Question: What is Two type Relationships?
- Modeling Embedded Relationships
- Modeling Referenced Relationships
Question: What is Modeling Embedded Relationships?
In this Approach, we embed the one document into other.
For Example:
{
"_id":ObjectId("521234cd85242f436000007"),
"contact": "987685555555",
"dob": "01-01-1992",
"name": "User1",
"address": [
{
"pincode": 160011,
"city": "Mohali",
},
{
"pincode": 140301,
"city": "Mohali",
}
]
}
We have added the both address in in user document.
Question: Modeling Referenced Relationships?
In this approach, We add the referenceId instead of whole document, thats why know as Referenced Relationships.
For Eample:
{
"_id":ObjectId("521234cd85242f436000007"),
"contact": "987685555555",
"dob": "01-01-1992",
"name": "User1",
"address": [
ObjectId("521234ce85242f436000001"),
ObjectId("521234ce85242f436000002")
]
}
Above is also know as Manual References because we put the RefereceId static.
Question: What is different between "Manual References" and "DBRefs References"?
Manual References
where you save the _id field of one document in another document as a reference know as Manual References.
Just see the above question for Example.
DBRefs References
References the one document with another using the value of first document's _id field, collection name, and database name.
Question: Explain the DBRefs References in detail?
There are three fields in DBRefs:
$ref: This field specifies the collection of the referenced document.
$id: This field specifies the _id field of the referenced document.
$db: Database name where referenced document lies. This is Optional.
{
"_id":ObjectId("521234cd85242f436000007"),
"address": {
"$ref": "address_home",
"$id": ObjectId("521234cd85242f436000007"),
"$db": "webtechnology"},
"contact": "987685555555",
"dob": "01-01-1991",
"name": "web-tech"
}
Question: What is a Covered Query?
Covered query is a query in which all the "fields of Query" and "fields returned in the query" are same index.
For Example:
{
"_id":ObjectId("521234cd85242f436000007"),
"address": {
"$ref": "address_home",
"$id": ObjectId("521234cd85242f436000007"),
"$db": "webtechnology"},
"contact": "987685555555",
"dob": "01-01-1991",
"name": "web-tech"
}
Add "Index"
db.users.ensureIndex({name:1,usercontact:1})
If we are using below:
db.users.find({name:"web-tech"},{usercontact:"987685555555",_id:0})
This is best example for Covered Query.
Question: How to Analyzing the queries in MongoDB?
$explain: The $explain operator provides information
on the query,
indexes and other
statistics. For analyse the use just add .explain() at the end of query.
db.users.find({name:"web-tech"}).explain()
Question: What is use of $hint operator?
$hint: operator is used to forces the query optimizer to use the specified index to run a query.
It is useful when you want to test performance of a query with different indexes with applying the index in real.
db.users.find({name:"web-tech"}).hint({dob:"01-01-2015"}).explain()
Question: Does MongoDb provides atomic transactions?
Yes, but provides only for single document.
In this way, Either it update all fields or none.
db.myproducts.findAndModify({
query:{_id:2,product_available:{$gt:0}},
update:{
$inc:{product_available:-1},
$push:{product_bought_by:{customer:"webtech",date:"19-Jan-2014"}}
}
})
Question: How to add index on document?
db.users.ensureIndex({"dob":1})
Question: How to add index on sub document?
db.users.ensureIndex({"address.city":1,"address.state":1})