Display information of mongodb - mongodb
Hi all I am very new in mongodb and I have some problems when I try to do some find in a collections I hope so someone can help me.
Imagine I have the following collection:
db.fechasCambio.insert(
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
},
{
'Divisa':'BGD',
'05/10/2015':'19558',
'02/10/2015':'19558',
'01/10/2015':'19558',
'30/09/2015':'19558',
'29/09/2015':'19558',
'28/09/2015':'19558',
'25/09/2015':'19558',
'24/09/2015':'19558'
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
'30/09/2015':'0',
'29/09/2015':'0',
'28/09/2015':'0',
'25/09/2015':'0',
'24/09/2015':'0'
}
]
}
) c
Now I want do some find inside the collections for example:
A-I want to bring me all currencies search by date range 05/10/2015 to 01/10/2015 then I need that return something like this:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
},
{
'Divisa':'BGD',
'05/10/2015':'19558',
'02/10/2015':'19558',
'01/10/2015':'19558',
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
B-Also I need to know how do a find that return all the information related with a currency, for example I want searh all releated with the currency 'USD', I need return something similar to:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
C-And finally I need to know how do a find that return all the information related with a value, for example I want search all the information with value of 0 between 12000, I need return something similar to:
{
columns:[
'Divisa',
'05/10/2015',
'02/10/2015',
'01/10/2015',
'30/09/2015',
'29/09/2015',
'28/09/2015',
'25/09/2015',
'24/09/2015',
],
data:[
{
'Divisa':'USD',
'05/10/2015':'11236',
'02/10/2015':'1116',
'01/10/2015':'11153',
'30/09/2015':'11203',
'29/09/2015':'11204',
'28/09/2015':'1117',
'25/09/2015':'11151',
'24/09/2015':'11241'
},
{
'Divisa':'CYP',
'05/10/2015':'0',
'02/10/2015':'0',
'01/10/2015':'0',
'30/09/2015':'0',
'29/09/2015':'0',
'28/09/2015':'0',
'25/09/2015':'0',
'24/09/2015':'0'
}
]
}
I want to know what is the structure of the find that I need to use for have a result similar.
Related
How to perform addition in already existing json object in mongoDB?
I want to add the cost of a particular month for a specific category. This is my object stored in MongoDB : { "2022":{ "January":{ "Food":30, "Traveling":0, "Medical":0, }, "Feburary":{ "Food":1000, "Traveling":0, "Medical":50, }, "March":{ "Food":100, "Traveling":20, "Medical":10, } } } Now, I am making a patch request: { "month":"March", "monthData":[70,45,100] } Now, I want my data to be updated like this: { "2022":{ "January":{ "Food":30, "Traveling":0, "Medical":0, }, "Feburary":{ "Food":1000, "Traveling":0, "Medical":50, }, "March":{ "Food":170, "Traveling":65, "Medical":110, } } } What query should I write using Model.findAndUpate(filter,object) so that above thing works.
Your patch request looks to me like this: db.collection.update({}, { "$inc": { "2022.March.Food": 70, "2022.March.Traveling": 45, "2022.March.Medical": 100 } }) Playgroud
How can I return the element I'm looking for inside a nested array?
I have a database like this: [ { "universe":"comics", "saga":[ { "name":"x-men", "characters":[ { "character":"wolverine", "picture":"618035022351.png" }, { "character":"cyclops", "picture":"618035022352.png" } ] } ] }, { "universe":"dc", "saga":[ { "name":"spiderman", "characters":[ { "character":"venom", "picture":"618035022353.png" } ] } ] } ] and with this code I manage to update one of the objects in my array. specifically the object where character: wolverine db.mydb.findOneAndUpdate({ "universe": "comics", "saga.name": "x-men", "saga.characters.character": "wolverine" }, { $set: { "saga.$[].characters.$[].character": "lobezno", "saga.$[].characters.$[].picture": "618035022354.png", } }, { new: false } ) it returns all my document, I need ONLY the document matched I would like to return the object that I have updated without having to make more queries to the database. Note I have been told that my code does not work well as it should, apparently my query to update this bad, I would like to know how to fix it and get the object that matches these search criteria. In other words how can I get this output: { "character":"wolverine", "picture":"618035022351.png" } in a single query using filters { "universe": "comics", "saga.name": "x-men", "saga.characters.character": "wolverine" } My MongoDB knowledge prevents me from correcting this.
Use the shell method findAndModify to suit your needs. But you cannot use the positional character $ more than once while projecting in MongoDb, so you may have to keep track of it yourself at client-side. Use arrayFilters to update deeply nested sub-document, instead of positional all operator $[]. Below is a working query - var query = { universe: 'comics' }; var update = { $set: { 'saga.$[outer].characters.$[inner].character': 'lobezno', 'saga.$[outer].characters.$[inner].picture': '618035022354.png', } }; var fields = { 'saga.characters': 1 }; var updateFilter = { arrayFilters: [ { 'outer.name': 'x-men' }, { 'inner.character': 'wolverine' } ] }; db.collection.findAndModify({ query, update, fields, arrayFilters: updateFilter.arrayFilters new: true });
If I understand your question correctly, your updating is working as expected and your issue is that it returns the whole document and you don't want to query the database to just to return these two fields. Why don't you just extract the fields from the document returned from your update? You are not going to the database when doing that. var extractElementFromResult = null; if(result != null) { extractElementFromResult = result.saga .filter(item => item.name == "x-men")[0] .characters .filter(item => item.character == "wolverine")[0]; }
MongoDB Get Multiple objects with the same name within objects with different name:
So I can't seem to get the results I want so I'm asking for your kind help on it. Imagine a MongoDB database built for multi-language content, something like this: "info": { "en": { "greetings": { "hello":"hello", "goodbye":"goodbye" }, "directions": { "left": "left", "right": "right" } }, "pt": { "greetings": { "hello":"olá", "goodbye":"adeus" }, "directions": { "left": "esquerda", "right": "direita" } } } Now if I want to query in order to get the directions object in both English and Portuguese but not the greetings, how should I do it? If it helps, my purpose is to subscribe to exactly that content on a meteor app's template, so no need for all other objects within a given language object, just the one I need for any given template. Thanks in advance!
how about you iterate? db.collection.find().forEach((language){ some code here })
WaterlineJS/SailsJS: Query "contains" array
how do i accomplish multiple value inside query 'contains' ? eg. { name: { contains: [ 'west', 'lob' ] }, address : { contains: 'york' } }
Having checked up on it via the gitter channel you need to do... If you wanna use contains then you need to use LIKE! if you want a straight match then use a normal query. Model.query("SELECT whatever FROM MyTable WHERE someFielding LIKE '%THEVALUE%'", function(error, myRows) { // do some stuff }); http://sailsjs.org/documentation/reference/waterline-orm/models/query
Sails Waterline "Or" clause not working
I tried a lot of time to figure out getting an OR clause working in sails without success. I am using Sails-MySql adapter. Have anyone of you done anything like this already? I would appreciate some help. Basically this is what I want to do: Do an OR clause on a set of fields along with an AND on another set of fields. Something like this: FdbDevice .find() .where(or: [ { deviceCategory: “cardiology valve bioprosthesis” }, { deviceCategory: “nephrology haemodialysis catheter” } ] }) .where( { catalogNumber : “Z286004” }, { modelNumber: “Z286004” } ) .exec
In this particular case, here is how I would do it: // Each element in the array is treated as 'or' var possibleDeviceCategories = [ 'cardiology valve bioprosthesis', 'nephrology haemodialysis catheter' ]; FdbDevice .find({ deviceCategory: possibleDeviceCategories, catalogNumber: 'Z286004', modelNumber: 'Z286004' }) .exec(cb); Check out the docs for more informations about the Waterline's query language.
you can try something like that, into the find: FdbDevice .find({or: [ { deviceCategory: “cardiology valve bioprosthesis” }, { deviceCategory: “nephrology haemodialysis catheter” } ] }) .where( { catalogNumber : “Z286004” }, { modelNumber: “Z286004” } )