Best way to create index for MongoDB - 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}

Related

mongodb lookup giving empty array

BED_MAST this is my one collection bed_mast contains WARD_ID and want to perform join to my other collection with is WARD_MAST given below.
{
"_id" : ObjectId("5e53c95a26b0e5ad0fb46376"),
"Bed_id" : "bd-10",
"WARD_ID" : "4",
"OCCUPIED" : "0",
"BED_TYPE" : "single AC"
}
{
"_id" : ObjectId("5e53c95a26b0e5ad0fb46377"),
"Bed_id" : "bd-11",
"WARD_ID" : "1",
"OCCUPIED" : "0",
"BED_TYPE" : "single Non AC"
}
WARD_MAST this is my WARD_MAST having ward_id. but while I am putting lookup I am not getting any data.
{
"_id" : ObjectId("5e53c95b26b0e5ad0fb46544"),
"patient_id" : null,
"ward_id" : 1,
"total_beds" : 55,
"ward_name" : "Ward 1"
}
{
"_id" : ObjectId("5e53c95d26b0e5ad0fb46545"),
"patient_id" : null,
"ward_id" : 2,
"total_beds" : 63,
"ward_name" : "Ward 2"
}
MY query is
db.BED_MAST.aggregate([{$lookup:{'from':"WARD_MAST",'localField':"WARD_ID",'foreignField':"ward_id",'as':"lookup_value"}}]).pretty()
output: I have confirmed the data by running this query to MySQL there it is working fine
{
"_id" : ObjectId("5e53c95b26b0e5ad0fb46388"),
"Bed_id" : "bd-28",
"WARD_ID" : "6",
"OCCUPIED" : "0",
"BED_TYPE" : "NICU",
"lookup_value" : [ ]
}
SAMPLE VALUES DATA IS GIVEN ALL DATA IS NOT POSSIBLE TO GIVE. I know it was asked 1000 times but not able to resolve this question. tried to solve with lookup. but it showing blank space. Is anything I am missing.
The problem is BED_MAST collection's WARD_ID has string values and WARD_MAST collection's ward_id has Number values.

Getting data range from firebase with swift

I would like to ask that question. The question is how to get specific data range from firebase ?
I have table on firebase like this:
"users" : {
"Jz3IpatRWiWoDbiYM62q6qbHB503" : {
"email" : "Kaanozdemir#gmail.com",
"lastName" : "Ozdemir",
"name" : "Kaan"
},
"PmeYYFiac0c55fU2sFpnTP308mC3" : {
"email" : "kevinhart#gmail.com",
"lastName" : "Hart",
"name" : "Kevin"
},
"r0bMqSGCWihFi2EF4u6ckSzLP8v1" : {
"email" : "Marcusalvarez#gmail.com",
"lastName" : "Alvarez",
"name" : "Marcus"
},
"A3tmSSGCWihFi2EF4u6ckSzLP8c1" : {
"email" : "taylorswift#gmail.com",
"lastName" : "Swift",
"name" : "Taylor"
},
"3SUTsiGCWihFi2EF4u6ckSzLP8v2" : {
"email" : "jimmyfellon#gmail.com",
"lastName" : "Fellon",
"name" : "Jimmy"
},
"lgSit3GCWihFi2EF4u6ckSzLP8u3" : {
"email" : "jaxteller#gmail.com",
"lastName" : "Teller",
"name" : "Jax"
}
For example, I would like to get users values between 2 and 4 [2 - 4](Marcus Alvarez - Taylor Swift - Jimmy Fellon).
Is there any way to do that server side ? I don't wanna get all data and pick values that I want. Anyone knows?
Change your JSON DB structure to include an index in every node :
"users" : {
"autoID1" : {
"email" :.....,
"lastName" : ......,
"name" :.......,
"index" : //e.g.. 1,2,3,4......
}
},
"noOfUsers" : 223,
If you are appending this users node via app, you have too keep track of the no of users in Database node users and keep updating the noOfUsers whenever a new user is added. And to set the next ones index number , just retrieve that node value i.e 223 and sees it and then increment the noOfUsers......
To retrieve between 2-4 .. Now you can use :
Database.database().reference().child("users").queryOrdered(byChild: "index").queryStarting(atValue: "2").queryEnding(atValue: "4").observe....

Mongodb Document size

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.

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.

using 2 different result sets in mongodb

I'm using groovy with mongodb. I have a result set but need a value from a different grouping of documents. How do I pull that value into the result set I need?
MAIN:Network data
"resource_metadata" : {
"name" : "tapd2e75adf-71",
"parameters" : { },
"fref" : null,
"instance_id" : "9f170531-79d0-48ee-b0f7-9bd2788b1cc5"}
I need the display_name for the network data result set which is contained in the compute data.
CPU data
"resource_id" : "9f170531-79d0-48ee-b0f7-9bd2788b1cc5",
"resource_metadata" : {
"ramdisk_id" : "",
"display_name" : "testinstance0001"}
You can see the resource_id and the Instance_id are the same values. I know there is no relationship I can do but trying to reach to see if anyone has come across this. I'm using the table model to retrieve data for reporting. Hashtable has been suggested to me but I'm not seeing that working. Somehow in the hasNext I need to include the display_name value. in the networking data so GUID number doesn't only valid name shows from compute data.
def docs = meter.find(query).sort(sort).limit(50)\
while (docs.hasNext()) { def doc = docs.next()\
model.addRow([ doc.get("counter_name"),doc.get("counter_volume"),doc.get("timestamp"),\
doc.get("resource_metadata").getString("mac"),\
doc.get("resource_metadata").getString("instance_id"),\
doc.get("counter_unit")]
as Object[]);}
Full document:
1st set where I need the network data measure with no name only id {resource_metadata.instance_id}
{
"_id" : ObjectId("528812f8be09a32281e137d0"),
"counter_name" : "network.outgoing.packets",
"user_id" : "4d4e43ec79c5497491b23b13644c2a3b",
"timestamp" : ISODate("2013-11-17T00:51:00Z"),
"resource_metadata" : {
"name" : "tap6baab24e-8f",
"parameters" : { },
"fref" : null,
"instance_id" : "a8727a1d-4661-4565-9c0a-511279024a97",
"instance_type" : "50",
"mac" : "fa:16:3e:a3:bf:fc"
},
"source" : "openstack",
"counter_unit" : "packet",
"counter_volume" : 4611911,
"project_id" : "97dc4ca962b040608e7e707dd03f2574",
"message_id" : "54039238-4f22-11e3-8e68-e4115b99a59d",
"counter_type" : "cumulative"
}
2nd set where I want to grab the name as I get the values {resource_id}:
"_id" : ObjectId("5287bc3ebe09a32281dd2594"),
"counter_name" : "cpu",
"user_id" : "4d4e43ec79c5497491b23b13644c2a3b",
"message_signature" :
"timestamp" : ISODate("2013-11-16T18:40:58Z"),
"resource_id" : "a8727a1d-4661-4565-9c0a-511279024a97",
"resource_metadata" : {
"ramdisk_id" : "",
"display_name" : "vmsapng01",
"name" : "instance-000014d4",
"disk_gb" : "",
"availability_zone" : "",
"kernel_id" : "",
"ephemeral_gb" : "",
"host" : "3746d148a76f4e1a8203d7e2378ef48ccad8a714a47e7481ab37bcb6",
"memory_mb" : "",
"instance_type" : "50",
"vcpus" : "",
"root_gb" : "",
"image_ref" : "869be2c0-9480-4239-97ad-df383c6d09bf",
"architecture" : "",
"os_type" : "",
"reservation_id" : ""
},
"source" : "openstack",
"counter_unit" : "ns",
"counter_volume" : NumberLong("724574640000000"),
"project_id" : "97dc4ca962b040608e7e707dd03f2574",
"message_id" : "a240fa5a-4eee-11e3-8e68-e4115b99a59d",
"counter_type" : "cumulative"
}
This is another collection that contains the same value but just thought it would be easier to grab from same collection:
"_id" : "a8727a1d-4661-4565-9c0a-511279024a97",
"metadata" : {
"ramdisk_id" : "",
"display_name" : "vmsapng01",
"name" : "instance-000014d4",
"disk_gb" : "",
"availability_zone" : "",
"kernel_id" : "",
"ephemeral_gb" : "",
"host" : "3746d148a76f4e1a8203d7e2378ef48ccad8a714a47e7481ab37bcb6",
"memory_mb" : "",
"instance_type" : "50",
"vcpus" : "",
"root_gb" : "",
"image_ref" : "869be2c0-9480-4239-97ad-df383c6d09bf",
"architecture" : "",
"os_type" : "",
"reservation_id" : "",
}
Mike
It looks like these data are in 2 different collections, is this correct?
Would you be able to query CPU data for each "instance_id" ("resource_id")?
Or if this would cause too many queries to the database (looks like you limit to 50...) you could use $in with the list of all "Instance_id"s
http://docs.mongodb.org/manual/reference/operator/query/in/
Either way, you will need to query each collection separately.