the mongodb documents structures are like:
No1.
"id" : 1,
"article": "go hiking and bbq",
"category": [
"travel",
"hiking",
"bbq"
]
No2.
"id" : 2,
"article": "I love cat and travel",
"category": [
"pet",
"cat",
"travel"
]
when i use mongoexport to export it to csv format, it will give me something like:
id article category
1 go hiking and bbq ["travel", "hiking", "bbq"]
2 I love cat and travel ["pet", "cat", "travel"]
which is "wide format" data structure
SO, I wonder if it's possible if it's possible to export as LONG FORMAT, I want to OPEN the category array, repeat each article per category. something like:
id article category
1 go hiking and bbq "travel"
1 go hiking and bbq "hiking"
1 go hiking and bbq "bbq"
2 I love cat and travel "pet"
2 I love cat and travel "cat"
2 I love cat and travel "travel"
or if mongodb do not have this function, is there any tools that could help me to transfer the exported csv file from wide format to long format? thanks
==== updated, Solutions here====
thx wizard. update mongodb up to 2.6.x.
first aggregate to a new collection, then export that collect
refer here
Related
How do I get random algolia item from it's index?
All of my items have:
objectID "POST#news#44.7704046#17.1900285"
name "News"
categories [ "cafe", "food", "establishment", "food" ]
_geoloc { lat: "44.7704046", lng: "17.1900285" }
I would like to optionally search by name, match 1 or all categories, geo location filtering with distance, and most importantly, I only want 1 RANDOM returned from Algolia.
I can't do client side random, because sometimes without filters I would get too many results back ( 10000 ), so I can't transfer that over the wire.
Please help
Hi #Djordje there are no real way to get a random result with Algolia though you you could use an attribute to randomise the results and only use the first item. See documentation [here][1]
I have a collection dinosaurs with documents of this structure:
doc#1:
name: "Tyrannosaurus rex",
dominantColors: [
"beige",
"blue",
"green"
]
doc#2:
name: "Velociraptor",
dominantColors: [
"green",
"orange",
"white"
]
I want to query the collection by color name (for example: green) to get documents sorted by color's position in dominantColors array. First get the documents in which green occurs higher in the array, then those in which it is lower. So, in the provided case I would get doc#2 first, then doc#1.
Each dominantColors array contains 3 elements, with elements sorted from most dominant to least.
I am looking through documentation, but am not able to find a solution. Maybe I need a different data structure altogether?
Cloud Firestore doesn't support querying arrays by ranked index. The only way you can query an array is using an array-contains type query.
What you could do instead is organize your colors using maps where the color is the key and their rank is the value:
name: "Tyrannosaurus rex",
dominantColors: {
"beige": 1,
"blue": 2,
"green": 3
}
Then you can order the query by the value of the map's property. So, in JavaScript, it would be something like this:
firebase
.collection('dinosaurs')
.where('dominantColors.green', '>', 0)
.orderBy('dominantColors.green')
Since in Scala lists are actually build like (here for List(1,2,3)) this:
[ 1 , [ 2, [ 3 , Nill ] ] ] // (pseudo-code)
it is more efficient to pretend new elements and that is why :: is right associative (all explained in https://stackoverflow.com/a/1162980/4533188) - to be better readable (here 1 :: 2 :: 3). That answers my question, why it's good to have right association in the first place. But why didn't the designers of Lists simply construct them like
[ Nill , [ 3, [ 2 , 1 ] ] ] // (pseudo-code)
internally and use conventional left association?
Here in a graphic what my pseudo-code is supposed to mean (since it does not show the "links" of the linked list):
Because an append-list wouldn't be immutable (or you'd have to copy it in its entirety on each change).
See https://en.wikipedia.org/wiki/Linked_list for mre.
In mongodb, which style is better? 1) or 2)? Can I retrieve only line name from 1) despite of getting whole record on db.record.find("line":"east")?
1.
{
"line": "east",
"station":[
{ "name": "ABC", "_id": "1", },
{ "name": "DEF", "_id": "2" }
]
}
2.
{ "line": "east", "l_id":"1"},
{"station":"ABC", "_id":"1", "l_id":"1"},
{"station":"ABC", "_id":"2", "l_id":"1"}
Note: line and station has one to many relationship.
If you are most commonly getting stations by line and often need all stations of a line alt 1 is the best. If you are commonly retrieving single stations or a subset of stations 2 is the best. Alt 2 can lead to more queries since mongo doesn't really have any relations, alt 1 can lead to larger reads and make it more difficult to keep the working set in RAM because of larger objects. Alt 1 also has a minor drawback if you change values in a station on multiple lines - then you have to update its instance in each of the line documents containg it.
To get a partial object, i.e. not all stations in alt 1, you can do a projection to filter out what you don't want. It still means reading the whole object into memory first so you wouldn't gain a lot in performance from doing that.
I have a table in Filemaker 11 which has fields: thingID, infoNumber (#), itemHistory. infoNumber displays the order in which we think the item history's happened (sometimes this is incorrect and needs to be rearranged).
thingID, #, itemHistory
Thing 1, 1, was with Adam
Thing 1, 2, was with Claire
Thing 1, 3, was with Ben
Thing 1, 4, was with Dave
I display these in a List View (ordered by infoNumber asc), and a user realises that it actually went "1,3,2,4", I want to have up and down arrows visible in order for users to switch them, i.e. clicking on the up arrow on the record with infoNumber=3 will set it to 2 and the old infoNumber=2 will be set as 3.
How can I write a script to switch these when the user clicks on a button in a list view?
My idea:
Set Variable[$clickedDown, infoNumber] #the record we click on's infoNumber
If [ $clickedDown != 1 ]
Set Field [ infoNumber, clickedDown -1 ]
# But how do I move to the record with infoNumber = clickedDown-1 ??
End If
The way I have done this, is to do several Finds, here is my solution for going one way.
# First you've clicked on something, record its current infoNumber
Set Variable [ $infoNumber, infoNumber ]
# Use -2 (arbitrary) as a temporary place holder
Set Field [ infoNumber = -2]
Error Capture [ On ]
Perform Find [ thingID = $thingID, infoNumber = $infoNumber - 1 ]
If [ Get (LastError = 401) ]
# No results then just re-search temporary and set it back to what it
was
Perform Find [ thingID = $thingID, infoNumber = -2 ]
Set Field [ infoNumber, $infoNumber ]
Else
Set Field [ infoNumber, $infoNumber ]
Perform Find [ thingID = $thingID, infoNumber = -2 ]
Set Field [ infoNumber, $infoNumber -1 ]
End If
# Go back to layout you were in before
I would be interested to know if there was a better way!
You could do this by using relationships. Create a new table occurrence (say, History Previous) and link it to the list layout's table occurrence (say, History) with the following predicates:
History::ThingID = History Previous::ThingID
History::infoNumber > History Previous::infoNumber
Sort the relationship by History Previous::infoNumber, descending.
This will provide you with a set of related History records that appear earlier in the list for the relevant Thing. The first record will be the immediately previous one, thanks to the sorting.
Now, when you run the script, you can:
If [ Count ( History Previous::ThingID ) > 0 ]
Set Variable [ $infoNumber, History Previous::infoNumber ]
Set Field [ History Previous::infoNumber, History::infoNumber ]
Set Field [ History::infoNumber, $infoNumber ]
End If
Note that, although the History Previous relationship may refer to multiple records, you can rely on the relationship sorting to provide you with access to the first record, both when getting data from it (in the Set Variable step) and setting data into it (in the first Set Field step).