MongoDB and Mongoose: How to retrieve nested Object - javascript

I have the following documents in my mongodb collection:
{
"current" :
{
"aksd" : "5555",
"BullevardBoh" : "123"
},
"history" :
{ "1" : {
"deleted" : false,
"added" : false,
"date" : "21-08-2014"
}
},
{ "2" : {
"deleted" : false,
"added" : false,
"date" : "01-01-2013"
}
},
"_id" : ObjectId("53f74dad2cbfdc136a07bf16"),
"__v" : 0
}
I have multiple of these documents. Now I want to achieve two things with my Mongoose/Express API.
Query for all nested "current" in each document and retrieve them as JSON objects like such: {"aksd":"5555","BullevardBoh":"123"},{..},{..}.
Retrieve all history revisions (1,2...) where "date" is smaller than a given date.
As you can clearly see this is a kind of versioning I am trying to implement. I would also be interested if this kind of data structure will get indexed by MongoDB and if there is possibly a better way. (e.g. with arrays inside objects?)
This isn't working in MongoDB:
db.ips.findOne({current.aksd: {$exists:true}});

I think the quotes around the field are missing here:
db.ips.findOne({current.aksd: {$exists:true}});
This should work instead:
db.ips.findOne({"current.aksd": {$exists:true}});

While Ritesh's reply was a step in the right direction, I rather wanted to fetch the current object literal and its members inside the document not the whole document.
1.) Query for all nested "current" in each document
db.ips.find({"current":{$exists:true}}, {"current":1});
This is giving back all nested documents where the aksd literal is present:
{ "current" : { "aksd" : "5555", "BullevardBoh" : "123" }, "_id" : ObjectId("53f74dad2cb3dc136a07bf16") }
...
2.) Retrieving history revisions where date is smaller then a given date:
db.ips.find({"history.date": {$lt: "01-01-2014"}},{history:{$elemMatch:{date: {$lt:"01-01-2014"}}}});
Giving back the wanted nested date literal(s):
{ "historie" : [ { "date" : "01-01-2013", "added" : false, "deleted" : false } ], "_id" : ObjectId("53faf20f399a954b2b7736b6") }

Related

Mongo script to update an object if its a certain value

I am trying to update a document in Mongo if it has a certain value within a field.
{
"_id" : ObjectId("myObject"),
"source" : "BW",
"sourceTableName" : "myTable",
"tableName" : "tier",
"type" : "main",
"fieldMappings" : [
{
"sourceField" : "tier_id", <~~~~ If this is "tier_id", I want to change it to "trmls_id"
"reportingField" : "bw_id",
"type" : "integer",
"defaultMap" : {
"shouldErrorOnConvert" : false
}
}
]
}
I tried something like
db.getCollection('entityMap').update({"sourceTableName":"myTable"}, {"fieldMappings.0.sourceField":"trmls_id":{$exists : true}}, { $set: { "fieldMappings.0.sourceField": "tier_id" } })
and I think it is failing on the $exists parameter I am setting.
Is there a more cleaner/dynamic way to do this?
The whole query I am trying to formulate is
In the table myTable
I want to check if there is a sourceField that has the value tier_id
If there is tier_id, then change it to trmls_id
Otherwise do nothing
If there is a similar StackOverflow question that answers this, I am happy to explore it! Thanks!
There is a syntax mistake and you can use positional operator $ in update part because you have already selector field in query part,
db.getCollection('entityMap').update(
{
"sourceTableName": "myTable",
"fieldMappings.sourceField": "tier_id" // if this field found then it will go to update part
},
{
$set: {
"fieldMappings.$.sourceField": "trmls_id"
}
}
)

Need help writing a mongo API query in Javascript(React) that needs info from multiple levels

So I have a collection called "contributors." Within that collection the objects look like this:
{
"_id" : ObjectId("5ef1f75f7e6fb579ed3e5e8a"),
"#attributes" : {
"cand_name" : "name",
"cid" : "",
"cycle" : "",
"origin" : "",
"source" : "",
"notice" : ""
},
"contributor" : [
{
"#attributes" : {
"org_name" : "National Beer Wholesalers Assn",
"total" : "20000",
"pacs" : "20000",
"indivs" : "0"
}
},
{
"#attributes" : {
"org_name" : "Alabama Power",
"total" : "15000",
"pacs" : "10000",
"indivs" : "5000"
}
},
... with 10 "#attributes" objects in the "contributor" array.
What I need to do is query this whole collection to find each cand_name object that includes a certain org_name, which I'm already uncertain of how to do (since every time I've tried to query with a string it replaces the spaces in my string with %20).
But then I need to display a table with all the cand_name objects that included that org_name with the total, pacs, and indivs numbers that correspond to that cand_name & org_name.
To be super clear here I'm trying to make a table that displays every politician that a company has given money to (and how much).
So through all this I'm looking in multiple levels of each object and need to display data from all those levels together in one table. I may just be overthinking this, but it seems like it's going to be a complicated query...
Help?
Figured this out! Ended up working with the backend guy on our project and he did it all on backend.

Is there a way to check for duplicate entries in values of an array in MongoDB?

I am trying to remove the duplicate values from a deeply nested array. I've put the structure of the database down below. I want to compare the location of the steps with eachother and check for duplicates. I was thinking about using db.collection.aggregate, but it becomes a problem when trying to search through all steps since { $unwind: '$mapbox.routes.legs.0.steps' } requires a specific index for the steps as far as i know.
"mapboxRoute" : {
"routes" : [
{
"distance" : 105375,
"duration" : 8261.3,
"weight" : 11365.5,
"weight_name" : "routability",
"geometry" : "iv||H_{f^w#b#AFDZHb#DTBL?B#LN`ALx#BPA#WPEBc#X[PIFADFb#T|AHd#DZL~##DF\\Hd#DVFVBPDL?BBL#DBFJb#FV?#BHDL#FKBa#`#MJx#y##FKBa#`#MJi#b#WN]ROLSPWkAFS#s#Mg#Lf#Ar#GRVjAIHKJ_#b#INEFI^CHMPA#]\\IFa#\\s#l#MJMLIHe#b#_#^w#j#KFEBk#d#EDEDEBEBEBKJKHEBMLIHEDQDQLGFMHTQGFMHq#d#e#^GFg#b#ABQNCBIFWRQNm#h#QNa#\\ONGF?FAZBJv#vDd#xBd#fBj#hA`A`Cf#lAz#xBd#r#Z\\PRE\\CXCPB`A?r#A|#EdAR?DqABmDC]Ge#Me#m#yA}#wBiBeEo#uAg#cBMa#oAgGAKOU{AnAIFKJNRNMnAeABJNt#BNb#pBb#tB#BH\\ZhADF\\p#FN`A`Cf#lAHTFLDNb#dALRV^BBVXWYVZRPLHHDHDHDJCRCLAPG~#UNGLEDCRGPGJEl#UJEREBAVA\\BLBHF##PPHJJJNPBDFLBBL^Qc#BBL`#DJBDHPDPHNDJHPFJ#BBDPRPRNNRPBBHFNPDJP\\BDNf##D`BlHHTBLP^T^BBRVFFDBVLj#VLF~#b#FBGCHDJDB#ZJJF`#PTJNJZN^ZJHVVRTFJRVr#bAHJf#v#DFDF#BJL`#n#l#~#RXT^T\\f#t#FH##LRLRjAjB`#r#r#hA~AhCVj#kD_Gr#hA~AhCVj#Zl#BFXj#`#z#Xp#N^HTXx#FPVz#Vv#Tt##Fd#xAPn#Pn##FNp#Px#DRJ`#TfATfAb#xCRrAHj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B#FADGDGBK?K#G#KFKrA{AJMHGZW\\SDAVMJC\\KDAxEaAlFiAVGXMHABFBBB?D#B?BADEBG^KvMmCXIh#O\\OFCFEZO^S\\SyAv#ZO^S\\Sp#e#h#g#f#g#DGl#s#n#}#BGBALUPYd#_ABGn#oABCR_#HMFMXe#n#iAJQ^s#O]Sa#IQEI]o#Yk#MUKOGKMOKMMKk#e#m#g#AAYSUSTRUSWS]WAAWW[Y_#c#Y[CC_#g#[e#CC[g#[g#AAUc#Yk#GMWi#Ym#Qa#EGQUIKCGIU_#cAg#wAEMSo#So#Uq#?CTt#Uq#?Cm#sBK]W{#]oAIa#K_#S}#U{#GYWqAG]i#kC]eBGWi#iDEUEUIi#a#_CMq#CQOaASmAW_BG_#YyB[yBK_AOsAS_BEe#WoCIcAIgARjCIcAIgAOoBi#qHe#cHa#wFAKKyAAOIeACa#Ec#Gy#Cc#OqBSeCEs#AIAiACgA?[Ac#Ae#CeAAs#?E?y##sA?q#?MB_AC~#BaADeA#SFgADk#?I#YBe#?O?O#a#?IA_#A]?CA[C]AQCc#?CCQAU}#TKDg#PG#s#Rk#LMDc#H]Hk#NKBODC?u#RGB}#T|#U}#TODMBe#LoCp#kD|#I#MBMBI?E?K#K?c#?w#GKAmBWsCi#aAIIAYGe#OGCCAECGE?ACICEACGEG?GBCBA#CD?DABAH?D#B#JBF?JCNE`#If#EVK`#Sn#ADKTQ^\\u#KTS^IREDEHORCBURQPA#STSVg#h#oArAA#a#b#a#b#]^[Zy#z#e#d#CDAHAJEECBONYXA#_#^MLGHCLAHAJ?B#L#R#PCRCNEHMFC#cANbAOcANeAPwAV",
"legs" : [
{
"distance" : 105336.1,
"duration" : 8259.5,
"steps" : [
{
"distance" : 132.5,
"duration" : 50,
"geometry" : "iv||H_{f^w#b#??AFDZ??H`#?#DT??BL???B#LN`A??Lx#??BP",
"name" : "Mariaplaats",
"ref" : null,
"destinations" : null,
"exits" : null,
"driving_side" : "right",
"mode" : "driving",
"maneuver" : {
"location" : [
5.117934,
52.089493
]
Does someone know how I can compare the locations of the steps?
At the end I stopped making a seperate file for connecting with mongodb and cleaning the database and started using the already existing API to create a call. In this call I looped through the steps in the database and mapped the location to a key. After that I check if that key already existed and pushed that index to a duplicate array.
Then I proceeded to loop through the duplicate list and spliced the duplicate entries from the list with steps. After that I ran the following function to update the entry: await routeSchema.updateOne({ _id: route._id }, { $set: route });

Meteor: Return only single object in nested array within collection

I'm attempting to filter returned data sets with Meteor's find().fetch() to contain just a single object, it doesn't appear very useful if I query for a single subdocument but instead I receive several, some not even containing any of the matched terms.
I have a simple mixed data collection that looks like this:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"name" : "Entertainment",
"items" : [
{
"_id" : ObjectId("57a38b5f2bd9ac8225caff06"),
"slug" : "this-is-a-long-slug",
"title" : "This is a title"
},
{
"_id" : ObjectId("57a38b835ac9e2efc0fa09c6"),
"slug" : "mc",
"title" : "Technology"
}
]
}
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e8"),
"name" : "Sitewide",
"items" : [
{
"_id" : ObjectId("57a38bc75ac9e2efc0fa09c9"),
"slug" : "example",
"name" : "Single Example"
}
]
}
I can easily query for a specific object in the nested items array with the MongoDB shell as this:
db.categories.find( { "items.slug": "mc" }, { "items.$": 1 } );
This returns good data, it contains just the single object I want to work with:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"items" : [
{
"_id" : ObjectId("57a38b985ac9e2efc0fa09c8")
"slug" : "mc",
"name" : "Single Example"
}
]
}
However, if a similar query within Meteor is directly attempted:
/* server/publications.js */
Meteor.publish('categories.all', function () {
return Categories.find({}, { sort: { position: 1 } });
});
/* imports/ui/page.js */
Template.page.onCreated(function () {
this.subscribe('categories.all');
});
Template.page.helpers({
items: function () {
var item = Categories.find(
{ "items.slug": "mc" },
{ "items.$": 1 } )
.fetch();
console.log('item: %o', item);
}
});
The outcome isn't ideal as it returns the entire matched block, as well as every object in the nested items array:
{
"_id" : ObjectId("570d20de3ae6b49a54ee01e7"),
"name" : "Entertainment",
"boards" : [
{
"_id" : ObjectId("57a38b5f2bd9ac8225caff06")
"slug" : "this-is-a-long-slug",
"name" : "This is a title"
},
{
"_id" : ObjectId("57a38b835ac9e2efc0fa09c6")
"slug" : "mc",
"name" : "Technology"
}
]
}
I can then of course filter the returned cursor even further with a for loop to get just the needed object, but this seems unscalable and terribly inefficient while dealing with larger data sets.
I can't grasp why Meteor's find returns a completely different set of data than MongoDB's shell find, the only reasonable explanation is both function signatures are different.
Should I break up my nested collections into smaller collections and take a more relational database approach (i.e. store references to ObjectIDs) and query data from collection-to-collection, or is there a more powerful means available to efficiently filter large data sets into single objects that contain just the matched objects as demonstrated above?
The client side implementation of Mongo used by Meteor is called minimongo. It currently only implements a subset of available Mongo functionality. Minimongo does not currently support $ based projections. From the Field Specifiers section of the Meteor API:
Field operators such as $ and $elemMatch are not available on the client side yet.
This is one of the reasons why you're getting different results between the client and the Mongo shell. The closest you can get with your original query is the result you'll get by changing "items.$" to "items":
Categories.find(
{ "items.slug": "mc" },
{ "items": 1 }
).fetch();
This query still isn't quite right though. Minimongo expects your second find parameter to be one of the allowed option parameters outlined in the docs. To filter fields for example, you have to do something like:
Categories.find(
{ "items.slug": "mc" },
{
fields: {
"items": 1
}
}
).fetch();
On the client side (with Minimongo) you'll then need to filter the result further yourself.
There is another way of doing this though. If you run your Mongo query on the server, you won't be using Minimongo, which means projections are supported. As a quick example, try the following:
/server/main.js
const filteredCategories = Categories.find(
{ "items.slug": "mc" },
{
fields: {
"items.$": 1
}
}
).fetch();
console.log(filteredCategories);
The projection will work, and the logged results will match the results you see when using the Mongo console directly. Instead of running your Categories.find on the client side, you could instead create a Meteor Method that calls your Categories.find on the server, and returns the results back to the client.

copyValue from another collection in MongoDb

Can i Copy Some Field from collection to another collection?
I want copy values of bar to foo collection, but i don't want type filed, and I want insert in foo e new _id e extra field (userId) ( then i use Node.js)
collection bar
{
"_id" : ObjectId("77777777ffffff9999999999"),
"type" : 0,
"name" : "Default",
"index" : 1,
"layout" : "1",
}
collection foo
{
"_id" : NEW OBJECT ID,
// "type" : 0, NO IN THIS COLLECTION
"userId" : ObjectId("77777777ffffff9999999911"),
"name" : "Default",
"index" : 1,
"layout" : "1",
}
I try with db.bar.copyTo("foo"); but copy entire collection
Actually that is probably your best option. But when you don't want the new field in your collection, then just remove it using $unset:
db.foo.update({ },{ "$unset": { "type": 1 } },false,true)
That will remove the field from all documents in your new collection in one statement.
In future releases from 2.6 and upwards you can also do this using aggregate:
db.bar.aggregate([
{ "$project": {
"userId" : 1
"name" : 1
"index" : 1
"layout" : 1
}},
{ "$out": "foo" }
])
The new $out method sends the output of the aggregation statement to a collection.
You can copy fields from one collection to another by using
db.copyFromCollection.find().forEach(function(x){
db.copyToCOllection.update(
{"_id" : ObjectId("53205a4a14952bee39f3376e")}, //some condition for update
{ $set:{parameterOfCopyToCollection:x.parameterOfCopyFromCollection}
});
});
Here we iterate through the data by using the above function.We iterate through the collection from which we want to copy the data and then inside the function we update the document to which we want to add/update that data.

Categories