How to view the source code for map/reduce in MongoDB? - mongodb

I have written 2 functions for mapping and one for reducing in MongoDB. When I run this command below, it returns only the results for the MapReduce Code. I would like to see the source code to make some changes.
db.mapreduce_result.find().pretty()
Thanks,

One can use toSource() to find the source code of any Javascript method.
The toSource() method returns a string representing the source code of the object.
Since mongo shell is a complete JS interpreter you can use this method.
In your case the command would be: db.mapreduce_result.find().pretty().toSource().
Example: You can see the result when I used it for db.collection.find() method:

Related

how to pass data in Zeppelin to DS3.js for Spark visualization

The graph options with Zeppelin are pretty basic. So I am looking for an example of how to do something simple, like a barchart, with ds3.js. From what I can tell that would be the best graphing library to use to create stunning graphs.
Anyway my question is how to pass data to the JavaScript code. With regular Zeppelin charts you write scala or other code and then save that in a dataframe. Then on the next line you use the %sql option and you can write a SQL command and then buttons appear to let you graph the data.
But what I have found looking on the internet is no indication that data created in the scala code section would be passed to the Angular section where you put the ds3.js code.
Some examples I found are like this one where all the html and Javascript is put in one giant print statement in the scala code https://rawkintrevo.org/2016/09/20/gelly-on-apache-flink/
And then there is an example like this one Using d3.js with Apache Zeppelin where the Zeppelin line is all JavaScript, but the data is just a locally created array.
So I need (1) an example and (2) some understanding of how RDDs ad Dataframes can be passed into the JavaScript code, which of course is on a different line that the scala code. How do you bring objects in the scala section of the notebook into scope for the Javascript section.
You can refer to zeppelin docs for a good getting-started guide to creating custom visualization. Also, you might want to check out the code of some of the built-ins viz.
Regarding how data from DataFrames are passed to js, I'm pretty sure z.show or %sql triggers dataFrame.take(${zeppelin.spark.maxResult}) which collects the RDD[T] as a Seq[T] object to the driver whose elements are then used to render graphs.
Alternatively if you have a javascript graph defined in another paragraph, you can also usez.angularBind("values", rdd.take(maxResult)) to send the data to the angular view. There's a really nice answer here on the subject which might help.
Hope you find this helpful.

Get Collections stats in MongoDB 3.2

In earlier versions say MongoDB 2.6, the DBCollection class has this method getStats().
DBCollection.getStats()
In the new 3.x versions , we have a new class
MongoCollection
and it has no method to get the statistics.
My question is how to get the statistics from the MongoCollection class
So I think I've found a solution for you. It's a bit hackish, but from what I was reading, I couldn't find any other way around it. I was reading resources from Mongo and they were saying they simplifed the driver a bit and reduced the amount of available methods for a collection. I would guess that getStats() probably got cut since it doesn't seem like something you would do often, at least not programmatically for most use cases anyways. So here's what you can do:
First, a MongoDatabase object will have a runCommand() method. 3.0 driver docs
If you look here, you will get a list of all the commands you can execute with runCommand().
One of those commands is collStats. Based on the documentation, it looks like you will want to pass run command a Bson object that has the following form:
{
collStats: <string>,
scale: <int>,
verbose: <boolean>
}
where the collStats is the string name of the collection for which you want stats. Scale is an optional field; you can read about it at the last link. Verbose defaults to false.
I don't know for sure that this will get you want you want, but it will at least get you pretty close. Let me know how it works out!

Pretty printing of output in pymongo

I am using pymongo driver to work with Mongodb using Python. Every time when I run a query in python shell, it returns me some output which is very difficult to understand. I have used the .pretty() option with mongo shell, which gives the output in a structured way.
I want to know whether there is any method like pretty() in pymongo, which can return output in a structured way ?
I want to know whether there is any method like pretty() in PyMongo
No PyMongo doesn't provide such method. It is only available in the shell.
You need to use the pprint function from the pprint module.
Actually you can also program it by yourself like:
db = connection.[dbname]
collection = db.[yourcollectionname]
for col in collection.find({}):
for keys in col.keys():
print ('{', keys, ":" , col[keys] , '}' )
I think this will be helpful or take it as an option.
I'm a bit new to this too but I might have found a viable answer for those who are looking. Libraries I'm using are pymongo, bson, json, from bson import json_util and from bson.json_util import dumps, loads
Where you want to print (or return) try:
print(loads(dumps(stringToPrint, indent=4, default=json_util.default)))
If your data is already using loads, you will not need loads in this statement.
If you want to use return leave out the first parentheses.
Example:
return json.loads(json.dumps(string, ..... )
If you imported loads and dumps you can leave out json..
I haven't tried (because this worked great for me) to alter the 'indent' value but if you don't like how the output looks, try changing that.
There is no direct method to print output of pymongo in a structured way.
as the output of pymongo is a dict
print(json.dumps('variable with out of pymongo query'))
this will serve your purpose i think
It probably depends on your IDE, not the pymongo itself. the pymongo is responsible for manipulating data and communicating with the mongodb. I am using Visual Studio with PTVS and I have such options provided from the Visual Studio. The PyCharm is also a good option for IDE that will allow you to watch your code variables and the JSON in a formatted structure.

Find & FindAll methods missing in MongoDB.Driver v1.3.1 for C#

I am beginning to use MongoDB with C# and through following a few tutorials I have found that the methods Find & FindAll no longer exist in the latest versions.
Could somebody explain why and also, how would I now get the same functionality using v1.3.1?
No, they should be. At least I not see them at master branch on git here line 1655. In release notes for 1.3.1 here I also can't find any breaking changes.
It seems you can't find them because you have created mongodb collection in different way then before. Basically there is two approaches:
First approach is to specify exact type of document when getting collection:
var collection = db.GetCollection<ICanSpecifyTypeHere>("name")
//then collection has Find and FindAll methods
var result = collection.Find(Query.And());
Second approach is to specify type of document at find method :
var collection = db.GetCollection("name");
//in this case you should use FindAs<TypeOfDocument> and FindAllAs<TypeOfDocument>
var result = collection.FindAs<ICanSpecifyTypeHere>(Query.And());
I suppose that you have declared collection as in second approach and because of this don't see Find and FindAll methods.

How to execute (Materialize ) a LINQ query call to a ServiceOperation returning just a string value

Up front, the guy on the keyboard here is way overdue on the sleep department. But also kinda desperate.
I have a Data-service [WebGet] method, setup like this.
[WebGet]
public string Finalize(string PayloadObject, string CltUUID, string Comment){..}
It return a simple string, depending on the outcome of the execution inside. This is working fine.
I call it like this
var res = base.ServiceRef.CreateQuery<DBcontext>("Finalize")
.AddQueryOption("PayloadObject", string.Format("'{0}'", builder.ToString()))
...
How do I get this query to materialize?
Usually one uses res.ToList(), ToArray() or .First(). If I do, I get an exception. Using .ToString() does not execute the query, just returns the url.
The query works, if I break in a line after this code and click 'View...' in the debugger, the query is executed, the method runs on the server.
It seems I just don't get it to run, what am I missing?? besides sleep
Thanks for any pointers
Regards, Andreas
I think you cannot use CreateQuery to call operation returning single value. You must use Execute instead. MSDN documentation also mentions that operations returning primitive types cannot use QueryOption which make sense because query option is used to define query for IQueryable. If you need to pass parameters try to use common WCF REST approach to specify template in WebGet and on client side use the URI with parameters correctly included.
You can also try to use SingleResultAttribute on your service operation.