Mongodb Document size - mongodb

A quick question please to be sure.
I have the following example of document in my collection guest
"_id" : "JM15061985",
"last_name" : "Michel",
"first_name" : "Justine",
"gender" : "Female",
"title" : "Mme",
"telephone" : 3375,
"mail" : "justine.michel#yahoo.com",
"language" : "French",
"birthday" : ISODate("1985-06-14T22:00:00Z"),
"status" : "VIP",
"company" : "Test",
"address" : [
{
"street" : "45 Avenue de Paris",
"city" : "Nice",
"zip_code" : "06072",
"country" : "France"
},
{
"street" : "12 square xvy",
"city" : "Toulon",
"zip_code" : "83072",
"country" : "France"
},
]
I know that one document ins Mongodb can't exceed 16Mb.
So my basics questions are :
What does 16Mb represents really? (any exemple maybe?)
In my example, is each address considered as a document or this is only one document?

16MB is the maximum size of the BSON-document that represents your document. This includes nested objects, like your address example, and also key names (not just the values).
There's also some overhead per document property, as explained here.
To check BSON document size for a particular JS object, and if you happen to use Node.js, you can use the bson module:
var BSON = new (require('bson')).BSONPure.BSON();
var bson = BSON.serialize(obj, false, true, false);
console.log('bson size', bson.length);
There should be similar solutions for other programming languages.

Related

Update multiple data in single array element using object id using mongodb

I am Very Beginner in mongodb.I have mongodb data with multiple array elements just update multiple data in single array element
"_id" : ObjectId("6217138f0607ea2e07e70b25"),
"phone_number" : "7645645676",
"email" : "test02#gmail.com",
"name" : "John",
"address" : "44th street Englan",
"devices" : [
{
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
"_id" : ObjectId("6217138f0607ea2e07e70b26"),
},
{
"ime_number" : "HHH555",
"device_name" : "Harry Potter",
"subscription_type" : "Monthly",
"validity" : 32,
"_id" : ObjectId("621714570607ea2e07e70b2b"),
}
]
Only This data
"ime_number" : "123456789012345",
"device_name" : "Test456356",
"subscription_type" : "Yearly",
"validity" : 365,
Changed data
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
Help me to solve this
I think you can use $ operator to do this.
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
Now you have two options, to either update the individual fields (only the ones you want to) or update the whole object. For both of them you need $set :
Just pass the query as first argument of your update query and the second argument is your update clause.
Collection.update({'devices.device_name' : 'Test456356'}, {
{ '$set': { 'devices.$': {
"ime_number" : "888844442222",
"device_name" : "Remote Device",
"subscription_type" : "Monthly",
"validity" : 30,
} } }
});
Note: above updates all the elements found with the query. If you want to be specific you can use updateOne. Just replace update with updateOne and it will update the first matched document

Does MongoDB takes the DBRefs inside a document as a full size document for the 16MB threshold?

For example, I have a document called Country that contains multiple DBRefs to another document called Cities. If each City document has the size of 8MB for example, I only can store two DBRefs in the Country document, or the DBRef is just a reference and is not taking the full size of the document referenced?
Compare the two different ways to store relational data:
Embedded data
Referenced data
Embedded Data:
Countries collection
(city information is embedded as an array of sub documents in country document)
{
"_id" : ObjectId("5e336c2bc0a47e0a958a2b9c"),
"name" : "France",
"cities" : [
{
"name" : "Paris",
"population" : 2190327
},
{
"name" : "Marseille",
"population" : 862211
}
]
}
{
"_id" : ObjectId("5e336c85c0a47e0a958a2b9d"),
"name" : "Germany",
"cities" : [
{
"name" : "Berlin",
"population" : 3520031
},
{
"name" : "Hamburg",
"population" : 1787408
},
{
"name" : "Munich",
"population" : 1450381
}
]
}
Referenced Data
(Cities are in separate collection)
Cities collection
{
"_id" : ObjectId("5e336cfdc0a47e0a958a2b9e"),
"name" : "Paris",
"population" : 2190327
}
{
"_id" : ObjectId("5e336cfdc0a47e0a958a2b9f"),
"name" : "Marseille",
"population" : 862211
}
{
"_id" : ObjectId("5e336d11c0a47e0a958a2ba0"),
"name" : "Berlin",
"population" : 3520031
}
{
"_id" : ObjectId("5e336d11c0a47e0a958a2ba1"),
"name" : "Hamburg",
"population" : 1787408
}
{
"_id" : ObjectId("5e336d11c0a47e0a958a2ba2"),
"name" : "Munich",
"population" : 1450381
}
Countries collection
{
"_id" : ObjectId("5e336c2bc0a47e0a958a2b9c"),
"name" : "France",
"cities" : [
DBRef("cities", ObjectId("5e336cfdc0a47e0a958a2b9e"), "mydatabase"),
DBRef("cities", ObjectId("5e336cfdc0a47e0a958a2b9f"), "mydatabase")
]
}
{
"_id" : ObjectId("5e336c85c0a47e0a958a2b9d"),
"name" : "Germany",
"cities" : [
DBRef("cities", ObjectId("5e336d11c0a47e0a958a2ba0"), "mydatabase"),
DBRef("cities", ObjectId("5e336d11c0a47e0a958a2ba1"), "mydatabase"),
DBRef("cities", ObjectId("5e336d11c0a47e0a958a2ba2"), "mydatabase")
]
}
Evaluation
When comparing these two we can see the size of the documents differ. When using references the size of the country document is smaller. This example is academic at best, but consider if the sub documents are large...
Size of documents (in bytes)
Embedded documents:
Object.bsonsize(db.countries.findOne({name: "France"}))
144
Object.bsonsize(db.countries.findOne({name: "Germany"}))
189
Referenced versions
Object.bsonsize(db.countries.findOne({name: "France"}))
166
Object.bsonsize(db.countries.findOne({name: "Germany"}))
224
Conclusions
Well, the whole point of this exercise was to show the embedded documents are heavier than referenced documents, but the sub documents are so small (in this example) it caused this experiment to show the opposite! The embedded documents are smaller than the references because ObjectIds are heavy (relatively). Consider if the embedded sub documents are large. This will sway the experiment the other way. You can experiment with different data sets to see the differences in sizes to help determine the best schema approach. Size limits are only one aspect of schema design. DBRef lookups are far slower than embedded documents because each sub document is essentially a query itself. Also, consider the idea of embedding an ObjectID as a raw data point instead of an actual DBRef - this could yield a smaller and faster approach. Some articles have described using this approach as a poor-mans DBRef and advise against DBRef.
Please add comments or questions and I will be happy to discuss!

Best way to create index for MongoDB

I am having records stored in mongo-db collection for customer and there transactions with below format:
{
"_id" : ObjectId("59b6992a0b54c9c4a5434088"),
"Results" : {
"id" : "2139623696",
"member_joined_date" : ISODate("2010-07-07T00:00:00.000+0000"),
"account_activation_date" : ISODate("2010-07-07T00:00:00.000+0000"),
"family_name" : "XYZ",
"given_name" : "KOKI HOI",
"gender" : "Female",
"dob" : ISODate("1967-07-20T00:00:00.000+0000"),
"preflanguage" : "en-GB",
"title" : "MR",
"contact_no" : "60193551626",
"email" : "abc123#xmail.com",
"street1" : "address line 1",
"street2" : "address line 2",
"street3" : "address line 3",
"zipcd" : "123456",
"city" : "xyz",
"countrycd" : "Malaysia",
"Transaction" : [
{
"txncd" : "411",
"txndate" : ISODate("2017-08-02 00:00:00.000000"),
"prcs_date" : ISODate("2017-08-02 00:00:00.000000"),
"txn_descp" : "Some MALL : SHOP & FLY FREE",
"merchant_id" : "6587867dsfd",
"orig_pts" : "0.00000",
"text" : "Some text"
}
]
}
I want to create index on fields "txn_descp", "txndate", "member_joined_date", "gender", "dob" for faster access. Can some one help me in creating index for this document? Will appreciate any kind of help and suggestions.
While creating the index there are a few things to keep in mind.
Always create the index for the queries you use.
Go for compound indexes whenever possible.
First field in the index should be the one with the minimum possible values.Ie, if there is an index with gender and DOB as keys, It is better to have {gender:1,dob:1}

mongodb: taking a set of keys from one collection and matching with another

I'm new to mongodb and javascript, and have been reading the manual, but I can't seem to put the pieces together to solve the following problem.. I was wondering if you can kindly help.
I have two collections "places" and "reviews".
One document in "places" collection is as follows:
{
"_id" : "004571a7-afe4-4124-996e-b6ec779db494",
"name" : "wakawaka place",
"address" : {
"address" : "12 ad avenue",
"city" : "New York",
},
"review" : [
{
"id" : "i32347",
"review_list" : [
"r123456",
"r123457"
],
}
]
}
The "review" array can be empty for some documents.
And in the "reviews" collection, every document in the collection represents a review:
{
"_id" : ObjectId("53c913689c8e91a5a9c4047f"),
"user_id" : "useridhere",
"review_id" : "r123456",
"attraction_id" : "i32347",
"content" : "review content here"
}
What I would like to achieve is, for each place that has reviews, get the content of each review from the "review" collection and store them together in another new collection.
I'd be grateful for any suggestions on how to go about this.
Thanks

want to merge two collection in mongo db using map reduce

I have two collection as bellow products has reference of user. i search product by name & in return i want combine output of product and user using map reduce method
user collection
{
"_id" : ObjectId("52ac5dd1fb670c2007000000"),
"company" : {
"about" : "This is textile machinery dealer",
"contactAddress" : [{
"address" : "abcd",
"city" : "52ac4bc6fb670c1007000000",
"zipcode" : "39as46as80"
},{
"address" : "abcd",
"city" : "52ac4bc6fb670c1007000000",
"zipcode" : "39as46as80"
}],
"fax" : "58784868",
"mainProducts" : "ads,asd,asd",
"mobileNumber" : "9537236588",
"name" : "krishna steels",
}
"user" : ObjectId("52ac4eb7fb670c0c07000000")
}
product colletion
{
"_id" : ObjectId("52ac5722fb670cf806000002"),
"category" : "52a2a9cc48a508b80e00001d",
"deliveryTime" : "10 days after received the ",
"price" : {
"minPrice" : "2000",
"maxPrice" : "3000",
"perUnit" : "5288ac6f7c104203e0976851",
"currency" : "INR"
},
"productName" : "New Mobile Solar Charger with Carabiner",
"rejectReason" : "",
"status" : 1,
"user" : ObjectId("52ac4eb7fb670c0c07000000")
}
This cannot be done. Mongo support Map Reduce only on one collection. You could try to fetch and merge in a java collection. Couple of days back I solved a similar problem using java collection.
Click to see similar response about joins and multi collection not supported in mongo.
This can be done using two map reduces.
You run your first MR and then you reduce out the second MR onto the results of the first.
You shouldn't do this though. JOINs are not designed to be done through MR, in fact it sounds like you are trying to do this MR with inline output which in itself is a very bad idea.
MRs are not designed to run inline to the application.
You would be better off doing the JOIN else where.