MapReduce in MongoDB: Changing the document structure of the reduction collection - javascript

Maybe this is something I am just missing, but is there any way to change the way the structure of a map reduce comes out in the new collection? For instance, currently I have:
{ "_id" : id_for_item,
"value" : { "item1" : 10,
"item2" : 12,
"item3" : 19
}
}
but I would like something like:
{ "_id" : id_for_item,
"item1" : 10,
"item2" : 12,
"item3" : 19
}
Any help would be appreciated.

No, currently it is not possible. But there is a jira case here. You can vote up and wait. But it for sure will not be done soon, because of even not planned.
If you accessing database though the any driver you can create method that will map m/r results to the structure you want.

Related

Mongodb: how to pass current date filter in aggregate pipeline from javascript

I'm trying to get a simple date-filtered aggregation working.
I have documents like:
{
"name" : "Article 1",
"expires" : <some date [type:datetime]>
},
{
"name" : "Article 1",
"expires" : <some other date [type:datetime]>
},
...
I'm trying to query them with an aggregate pipeline over a call from javascript, so my pipeline is passing over as JSON.
I want to get all unexpired documents, but I haven't found a way to use the $$NOW variable that works for me.
var pipeline = [
{'$sort' : {[name] : 1}},
{ "$match": {
"expires": {"$gt": "$$NOW"}
}
}
];
await myGetData(collection, pipeline).then(resp => ...
I've also tried variations of $dateFromString without any success. This should be simple, so I assume I'm doing something dumb.
The issue seems to be getting the correct date format into the pipeline JSON. From other stackoverflow questions, it seems that it needs to be a data object for comparison? Not sure how to describe that in the JSON though.
Could someone please point me in the right direction?
Thanks

Is it possible to move a particular JSON key to the top of the JSON using Javascript/VuejJS/Nodejs package?

I have a web application built using the Vuejs within that I am obtaining a JSON from the Java backend service which has a format something like this:
{
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B",
"schema" : "2.0"
}
Before displaying the result to the user I would like to move the schema to the top so that it would be easy for the user to read and I want it to look something like this:
{
"schema" : "2.0",
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B"
}
As we can see only schema position has changed, the rest of the JSON as it is.
Please Note:
I am aware the JSON order does not matter but I am doing this for better readability purposes. If there is a way then it would be really useful for the reader to understand the JSON better.
I want to know if there is a direct way to do it rather than looping over the JSON, as my created JSON in the real application can be pretty large.
All I want to do is move the schema to the top of the JSON. The rest of the JSON can be as it is I do not want to make any modifications to it.
Is there a way to do this using vanilla Javascript or using some Nodejs library as I am using the Vuejs?
I would really appreciate it if there was a way to do it or is there any workaround for this.
A very simplistic approach could be to stringify a new object.
const myObject = {
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B",
"schema" : "2.0"
};
console.log(
JSON.stringify({
schema: myObject.schema,
...myObject
}, null, 2)
);
My suggestion is almost the same as the accepted answer but without using JSON.stringify():
const myObject = {
"a" : "Value-A",
"c" : "Value-C",
"b" : "Value-B",
"schema" : "2.0"
};
const myReorderedObject = {
schema: '',
...myObject
};
console.log(myReorderedObject);

MongoDB/NodeJS query to get data from dictionary

Hi in mongo DB I have a table "games" like this:
{
"_id" : ObjectId("53c66f922e15c4e5ee2655af"),
"name" : "alien-kindergarden",
"title" : "Alien Kindergarden",
"description" : "Alien description",
"gameCategory_id" : "1",
"deviceOrientation_id" : "1",
"position" : "1"
}
and I have a few dictionaries (also simple collections in MongoDB) like "gameCategory" for example:
{
"_id" : 0,
"name" : "GAME_CATEGORY_NO_CATEGORY"
}
{
"_id" : 1,
"name" : "GAME_CATEGORY_POPULAR"
}
How to get data from collection "games" with fields from my dictionary like:
{
"_id" : ObjectId("53c66f922e15c4e5ee2655af"),
"name" : "alien-kindergarden",
"title" : "Alien Kindergarden",
"description" : "Alien description",
"gameCategory" : GAME_CATEGORY_NO_CATEGORY, <---------------
"deviceOrientation_id" : "1",
"position" : "1"
}
Thanks. I'm using Node server for it.
What you are essentially asking for is a "JOIN" as in SQL. Your first point of reading should be that MongoDB does not do joins. The general concept here is "embedding" where the related information is actually contained within the document.
A good reading of the Data Modelling section of the official documentation can cover various points such as this and alternate approaches. But in most cases the data you wish to reference should just be part of the original document:
{
"_id" : ObjectId("53c66f922e15c4e5ee2655af"),
"name" : "alien-kindergarden",
"title" : "Alien Kindergarden",
"description" : "Alien description",
"gameCategory" : "GAME_CATEGORY_POPULAR",
"deviceOrientation_id" : "1",
"position" : "1"
}
This is generally because the MongoDB concept of being "scalable" is that all operations only ever deal with one collection at a time and "JOINS" are not attempted.
There are options available under node.js such as Mongoose that allow you to .populate() items from another "related" collection. But the general problem here is that you cannot "query" on the "related" information. All this really implements is a "query behind the scenes" approach. So more than one query is actually executed. To find by "related" information the best approach is generally:
var catId = db.category.findOne({ "name": "GAME_CATEGORY_POPULAR" })._id;
db.category.find({ "gameCategory_id": catId })
As nothing will let you query the "game" collection by a value held in a foreign collection.
The idea of "embedding" and generally "duplicating" data might seem alien to those used to relational database concepts. But really your reason for applying a solution such as MongoDB should be that you realize certain "relational patterns" are not the "best fit" for your application.
If you have not looked at this in that way, then perhaps you should stick with the relational database approach and use those tools. At least until you "find" the actual shortcomings and realize why you need to "design" around that.
Unlearn what you have learned.

AngularJS $scope data across different pages

I'm getting to grips with AngularJS and I am working on an application just now where I have questions and answers.
The questions use an incrementing + and - button per item which updates the $scope
What I am wondering though, because I will have to access the values from the question side of the app in the answers, what would be the simplest way to get this across. I had thought of storing the $scope.questions into localstorage.
{
"uid" : 1,
"name": "Quiz",
"drinks" : [
{
"id" : 0,
"type" : "Footballs",
"image" : "http://placehold.it/280x300",
"amount" : ""
},
{
"id" : 1,
"type" : "Golf Balls",
"image" : "http://placehold.it/280x300",
"amount" : ""
}
]
}
The above is json which is fed into my page and then using ng-repeat it will display and then the amount keys get updated when the user has clicked either + or -.
I would like to somehow update this json to be accessable throughout the site too so that when the user/client has updated it they can view a separate page which shows them the answers.
Used localStorage to carry data from the $scope around
localStorage.setItem('data', angular.toJson($scope.data));

scrape dechtech website using python

I am looking for a way to scrape the data from this website: http://www.dectech.org/football/index.php preferably using Python. The difficulty that I seem to be having is that the data is not hard-coded into the HTML of the website, and appears to be wrapped in something called a mochi-kit ( http://mochi.github.com/mochikit/ ).
I've done some research and it seems that something like BeautifulSoup might be useful to me, but I think I may not be using it correctly. I've also tried using urllib to parse the website with no joy.
My ultimate goal is to have a program that monitors the dectech website and when new predictions are released, automatically picks out value bets using the Betfair API.
It looks like the data is being loaded by javascript from this url
http://www.dectech.org/cgi-bin/new_site/GetUpcomingGames.pl?divID=0
which returns
{
"games" : [
{
"apct" : 0.377838,
"dpct" : 0.263445,
"expGoalDiff" : -0.04086,
"awayID" : "6",
"homeID" : "17",
"date" : "20/10/2012",
"away" : "Chelsea",
"home" : "Tottenham",
"hpct" : 0.358717
},
{
"apct" : 0.237829,
"dpct" : 0.250146,
"expGoalDiff" : 0.594234,
"awayID" : "1",
"homeID" : "8",
"date" : "20/10/2012",
"away" : "Aston Villa",
"home" : "Fulham",
"hpct" : 0.512025
}, /* shortened for brevity */
So you're incredibly lucky, you don't need to scrape the data (which is tricky), you just need to retreive it and parse it like they're doing with mochi.
Python's simplejson module would be able to parse it...

Categories

Resources