Hello Everybody i'm so sorry to ask but i've been trying to look for an answer for MONTHS, and im already desperate because i can't seem to find what i need till the point that i'm almost giving up with NoSQL.
So here's my Schema.
I have States of a Country and they have their non-repeatable ISO CODE,
{
code: 'US-NY',
name: 'New York',
cities: [ ]
},
etc ..
.. however inside the state (nested objects) we have cities:
cities:[
{
code: 'new-york',
name:'New York',
},
{
code: 'bufalo',
name:'Búfalo',
},
]
So when i add a document, i want to avoid duplicated nested objects (cities) with the same code (example: 'bufalo') within the same document (state).
BUT i could use 'bufalo' in another document (state).
When i set an index to cities.code to be Unique, it applies in the whole collection, and i want to use that code in another document.
I would like to repeat 'bufalo' city code in another document (state), but i dont want it to be repeated in the same document.
How could i archive this? Thank you so much for your kind help, i will be checking this post desperately.
Thanks
Alan D.
Related
Can someone give me an example of how to use the typeahead to autofill a search field
and put it in the URL /$typedata/ where it calls an API and returns a JSON file that looks like the below? All of the examples I see are nice and neat with the booktitle: "Leaving";
author: "whoever";
publisher: "Phantom";
or there is a list with the variables already identified ["Leaving", "whoever", "Phantom"]
I can't figure out how to put all of these elements together. I need to extract the "compound: []
up to six variables.
It's several different pieces and I'm new. I can't seem to get past the compound with mutliple variables. I know I need an index and to call the index by number but I can't seem to get it all together.
Any help is appreciated.
{
"status": {
"code": 0
},
"total": 6,
"dictionary_terms": {
"compound": [
"aspirin",
"Aspirina",
"AspirinTest2",
"ASPIRIN (MART.)",
"ASPIRIN COMPONENT OF AXOTAL",
"ASPIRIN COMPONENT OF AZDONE"
]
}
}
btw: I'm new to this and I'm sure I'm missing something important. Thanks for any help!!!
I havent really ran into a error, but I was wondering. MongoDB calls each thing inside a collection a "document" I was wondering what you call the things inside a document.
Ok, after looking for a long time. This is what you call the things inside a document. Its called a field.
#this entire thing is called a document.
#The fields in this document are: name, age, status, and groups.
#The values in this document are: "sue", 26, "A", and ["news", "sports"]
Hope this helps anyone who had the same question as me!
{
name: "sue",
age: 26,
status "A",
groups: ["news", "sports"]
}
Lets say I have a document structured like that :
datas: {
foo: {
...
keytoupdate: [...]
},
whatever: {
...
keytoupdate: [...]
},
anystring: {
...
keytoupdate: [...]
},
...: {
...
keytoupdate: [...]
}
}
I know that :
Each direct child property of the "datas" document has a "keytoupdate" field.
The direct child properties of the "datas" document varies from case to case: not necessarily the same name, neither the same number.
I want to update each "keytoupdate" fields, no matter how many of them there are.
The question is: How can I do that ? Is there any magic operator like $ that does the same job for Array ?
Thank you !
I'll answer my own question : there is no way to do that, we can't play with dynamic keys, just forget about it ! But there are 2 workarounds :
The best solution, as suggested by #chridam, is to redesign the schema to make an array of objects, where the keys are parts of the arrays, you can see this question for more details.
If you can't, the other (but not good) solution is to make a request for each field that might be in your document, instead of trying to do this in one request. This is a very bad solution, especially if your document may have lots of fields, and you have to known which fields that could be in your documents. This is a bad solution, absolutely not optimized, but it has the merit of being simple to implement
For learning purposes, I am building an app with basic functionality. This is the first time I am using MongoDB and I am wondering whether my data model makes sense or whether I should model it differently.
Basically, this is what should happen:
I have user accounts, they can create "tasks" and execute them. Each time they execute a task, the timestamp should be logged. They can then view a log (journal) which shows all the dates at which they executed a particular task.
My data model looks like this. Does this make sense?
I am especially wondering about the array of dates - is this the right way to go? (the log-Array might become quite long)
{
_id: "51d951922cf9f30200000004",
user: "jon doe",
password: "xxxxxxxxxx",
tasks: [
{
name: "one task",
log: [
ISODate("2010-10-10T20:55:36Z"),
ISODate("2011-10-10T20:55:36Z"),
ISODate("2012-10-10T20:55:36Z")
]
},
{
name: "another task",
log: [
ISODate("2010-10-10T20:55:36Z"),
ISODate("2011-10-10T20:55:36Z"),
ISODate("2012-10-10T20:55:36Z")
]
},
{
name: "one last task",
log: [
ISODate("2010-10-10T20:55:36Z"),
ISODate("2011-10-10T20:55:36Z"),
ISODate("2012-10-10T20:55:36Z")
]
}
]
}
What you have is probably fine. As you grow the sizes of the arrays you might run into some delays in copying large arrays, but there isn't really a space-saving alternative for the logs. The only thing I can think of is a nested document. That will probably update slightly faster once the size is huge, but it will take up more space and won't be nearly as nice or as readable. Rather than having an array with the logs, you would have a document like
{ 0: ISODate("2010-10-10T20:55:36Z" ,
1: ISODate("2010-10-10T20:55:36Z" ,
2: ISODate("2010-10-10T20:55:36Z" }
etc. This will update faster than creating a whole new (larger) array when a log is added, but also take up more space.
If it helps, MongoDB has a page on data modeling with some links you might find useful. http://docs.mongodb.org/manual/core/data-modeling/
So i need some advice as to what i'm doing incorrectly.
My database is setup up exactly like a file system consisting of folders and files.
It begins with a folder, but can have a relatively infinite number of subfolders and or files.
{
"name":"folder1",
"uniqueID":"zzz0",
"subcontents": [ {"name":"subfolder1", "uniqueID":"zzz1"},
{"name":"subfile1", "uniqueID":"zzz2"},
{"name":"subfile2", "uniqueID":"zzz3"},
{"name":"subfolder2", "subcontents": [...etc...], "uniqueID":"zzz4"},
]
}
Each folder/file document have a uniqueID so that I can reference to it (seen above zzz#). My question is, can I make a mongoDB query to pull out only a single document?
Like say for example db.fileSystemCollection.find({"uniqueID":"zzz4"}) and it would give me the following result? Do i have to use indexes to do this? I've been trying but the query returns empty every time.
intended result ---> {"name":"subfolder2", "subcontents": [...etc...], "uniqueID":"zzz4"}
[EDIT]
Based on the responses below, I will consider an XML database instead on mongoDB. The json structure cant be rearranged to work with MongoDB (too much data).
Short answer is no, as it's stated by Chris.
Your embedded representation of a tree is really good for intuitive understanding (and implementation as well). But if you want to allow effective searches on your tree using indices in MongoDB, you might consider another ways for tree storage. A bunch of ways is listed at http://docs.mongodb.org/manual/tutorial/model-tree-structures/
Please keep in mind that every representation has its own pros and cons depending on your access patterns.
Since for filesystem-like structure it's likely to have the ability to find all the sub contents of a given folder, you may use child references pattern for this:
{
"name":"folder1",
"uniqueID":"zzz0",
"subcontents": [ "zzz1",
"zzz2",
"zzz3",
"zzz4"
]
}
{
"name":"subfolder1",
"uniqueID":"zzz1"
}
...
No; searching for {uniqueID: "zzz4"} will only get you documents whose top-level uniqueID matches.
What you probably want is to maintain an array on the document which lists all the unique IDs in that tree. So your document would be:
{
"name":"folder1",
"uniqueID":"zzz0",
"idList": ["zzz0", "zzz1", "zzz2", "zzz3", "zzz4"],
"subcontents": [ {"name":"subfolder1", "uniqueID":"zzz1"},
{"name":"subfile1", "uniqueID":"zzz2"},
{"name":"subfile2", "uniqueID":"zzz3"},
{"name":"subfolder2", "subcontents": [...etc...], "uniqueID":"zzz4"},
]
}
Then you can index that:
db.fileSystemCollection.ensureIndex({"idList": 1})
Then you can find on it:
db.fileSystemCollection.find({"idList": "zzz4})
That'll return you those documents.
As an aside, if you're trying to store files in Mongo, have you looked at GridFS?