970_90

Wednesday, 28 September 2016

MongoDB indexing and Embed document

MongoDB indexing and Embed document

Question: How to create indexing on single column?
db.users.ensureIndex({"title":1})
1- Indexing in Ascending Order.


Question: How to create indexing on multiple column?
db.users.ensureIndex({"title":1,"description":-1})
1- Indexing in Ascending Order.
-1- Indexing in Descendig Order.


Question: How to make unique key in MongoDB?
db.users.ensureIndex({"username":1},{unique:true,sparse:true})
Now you can't add duplicate username in users collection.


Question: What is sparse in MongoDB indexing?
Sparse indexes only contain entries for documents that have the indexed field. The index skips over any document that is missing the indexed field. The index is sparse because it does not include all documents of a collection. .


Question: How to create indexing in background process?
db.users.ensureIndex({"title":1},{background:true})



Question: How to create indexing on column which can have duplicate values?
db.users.ensureIndex({"title":1},{dropDups:true})



Question: How can we set index name while creating indexing?
db.users.ensureIndex({"title":1},{name:'bankdb_users_index'})



Question: How to drop an index on single column?
db.users.dropIndex({"title":1})



Question: How to drop all indexes on document?
db.users.dropIndexex()



Question: How to update all index in collection?
db.collection.reIndex()



Question: How to create indexing in Embed document?
db.users.ensureIndex({"profile.city":1})

Search Document From Embed document
db.users.find( { "profile.city" : "newYork" } )



Question: Compare "Multiple collections" vs "Embedded documents"?
  1. No much differences for insertion and updates document.
  2. Separate collections are better when you need to select individual documents and gives more control over querying.
  3. Embedded documents are better when embed document is less OR No More.
  4. Getting record from embedded documents is easy as compare Multiple collections.
  5. Embed document limit is 16MB
Question: What is Relationships in MongoDB?
Relationships denotes how various documents are logically related to each other.


Question: What are different ways to achieve the relationships in MongoDB?
Following are various two Approaches
1) Embedded Approaches 2) Referenced Approaches


Question: Given an example of Embedded?
[
  {
    "_id": ObjectId("52ffc33cd85242f436099901"),
    "contact": "987654321",
    "dob": "01-01-1991",
    "name": "Tom Benzamin",
    "address": [
      {
        "pincode": 123456,
        "city": "Los Angeles",
        "state": "California"
      },
      {       
        "pincode": 456789,
        "city": "Chicago",
        "state": "Illinois"
      }
    ]
  }
]



Question: Given an example of Referenced?
[
  {
    "_id": ObjectId("52ffc33cd85242f436099901"),
    "contact": "987654321",
    "dob": "01-01-1991",
    "name": "Tom Benzamin",
    "address": [
        ObjectId("63ffc4a5d85242602e000000"),
        ObjectId("63ffc4a5d85242602e000001")
    ]
  }
]



1 comment:

  1. How to get records in Embed document?

    var comments = db.comments.find({user: 'rama'}, {post_id: true})
    var postIds = comments.map(function(c) { return c.post_id; });
    db.posts.find({_id: {$in: postIds}}, {title: true});

    ReplyDelete