Hot answers tagged

13

The power of the aggregation framework is it's ability to iterate over the dataset in various useful ways without incurring extra round trips between the database and the app. Your code uses one stage for aggregation (grouping by id1), and then jumps out of aggregation to iterate over the entire FUNDS collection for every user. That is very expensive, ...


12

You can avoid almost all of your redundancy by writing a function that accepts the status code as parameter. I defined a collections.defaultdict dictionary for the two status codes where there is some additional error message. For all other keys, this dictionary will return the default value (a str() == "" in this case). I also put a if __name__ == "...


9

The code looks pretty good. There are just a few stylistic things I would do differently. 1. Nested Describes You have quite a few tests which essentially test the same method of your model. Specifically, your tests which call functions like: saveLinkWithNoUser are all testing the Link.save. In cases like this, you can further organize your tests to be ...


8

I'm not 100% sure there is a particularly elegant way to write this. If the code did not have a different body for each if statement, one could rewrite it like this FORM_VALUES = { "priority": accident.priority, "status": accident.status, "duration": accident.duration, "service_status": accident.service_status } for key in FORM_VALUES: ...


8

I'd like to see this as my goal:1 if accident.status == 1: transfer_from_form(request.form, accident, [ ('reason', str), ('note', str), ]) else: transfer_from_form(request.form, accident, [ ('priority', int), ('status', int), ('duration', int), ('service_status', int), ]) Let's see what it ...


7

Don't forget that dict.get() can also take a default: if request.form.get('reason', accident.reason) != accident.reason: accident.set_reason(request.form['reason'], current_user) ...that makes it a little shorter. EDIT: or even def needs_update(name): return request.form.get(name, getattr(accident, name)) != getattr(accident, name) if ...


7

I'm the lead engineer for the MongoDB Perl driver with a couple of thoughts for you: you're using 'authenticate', which is for a very old version of the driver which is not recommended for use. In the v1.x series, you should provide username/password in the URI or in MongoClient parameters. Most of the value of this module seems to be setting parameters ...


6

Interesting question, it was hard to find things I would do different. To Malachi's point, there are a few strings you could put in a config object, I would definitely add also 10000 to that config object as config.delay and your statuses like 'ONGOING'. Also, I would write this: .then(function(results) { if (results.length > 0) return results[0]; ...


6

I'm not familiar with MR best practices, but here are some JS-specific comments. Key Points DRY - that emit code is repeated all over the place and has only slight changes - parameterize what varies and put it all in a function You are repeating your loops - either do everything in one loop for better performance (and keep in mind that for is faster than ...


5

A short review; Consider using JsHint Declare data with var, otherwise you are polluting the global namespace Consider err more often, the assumption that there will be no error will bite you Do not simply log to console.log, it is one of the most common bottlenecks Remove your commented out code, it keeps things clean My assumption is that The meta data ...


5

The logic for getting 100 posts is duplicated for the case of the initial load and subsequent loads with an offset. It would be better to refactor in a way to eliminate the duplicated logic: params = {'number': 100} offset = 0 while True: response = requests.get(url, params=params).json() found = response['found'] posts = response['posts'] ...


5

You should use is to compare to None, as that's faster. def is_user_in_db(user_id): return get_user_from_db(user_id) is None You can also use list concatenation rather than list.extend as it's slightly faster and there's no benefit to extend in this context. I also second the recommendation to use enumerate rather than having a manually incremented ...


5

docker-machine provides a simple way to get the IP of the host machine mongo --host `docker-machine ip default` --port 27017 I'm using default here, since that's the name of the machine I'm using.


5

Neither MongoDB's BasicDBObjectBuilder nor your UserDocumentBuilder is a Builder according to the pattern of the GoF. Your name UserDocumentBuilder is somehow confusing. Especially when you then define: BasicDBObjectBuilder userDocumentBuilder = new BasicDBObjectBuilder() It represents a builder for a user, not a builder for a document of a user, doesn't ...


5

Now isn't now You call now at the top of the program. First of all, it's never used, so it should be deleted. Even if it were used, this call should be moved next to the usage so that any delay between program start and the usage of this variable won't introduce error. Use what Python gives you In this case, os.environ["api_news"] is more easily expressed ...


5

First, you need to consider that adding 271 million records is going to be long, there's no changing that. You should also consider your processing power. You could have the best code in the world, if it runs on an average computer it's going to be average. Now, what can be done better? What you're doing is called bulk insertion. There are mechanisms with ...


4

It seems that you are storing the data with double links (article -> category AND category -> articles). I assume that you need to report on articles for a category. I would simple create an index on Category like this: var articleSchema = mongoose.Schema({ category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category', index: true }, title: ...


4

First, read about how .get() can chain multiple callbacks, then read how the sample code of Passport uses ensureAuthenticated as a callback. That should give you enough inspiration. Furthermore: English only in source code, especially for variables, even for comments Comments, your code could use more of them, and less, this comment is pretty useles: //...


4

You have some magic strings: return amqp.connect('amqp://localhost') this.mongo.db = mongo('mongodb://127.0.0.1:27017/test', ['test']); These strings should be kept in a high scope variable, so that you can easily maintain them by changing out the strings in one place. Eventually you will have bigger applications where you use the connection in several ...


4

Your code follows most of PEP8. However: Lines should be a maximum of 79 characters. The exception to this is comments, at 72. Operators should have a space either side of them. 2 * 2. The exception to this is to show precedence: 2*2 + 2. You should not have any spaces around the = when passing arguments: response = requests.get(url, params= { Your ...


4

You can reduce the number of commands in it. First of all, it's easy to combine these two commands: awk '{print $5}' | sed -n '2p' This is exactly the same: awk 'NR == 2 {print $5}' It's also easy to combine the two other sed commands: sed 's/tcp:\/\///' | sed 's/\:2376//' Like this: sed -e 's/tcp:\/\///' -e 's/\:2376//' By doing so you reduce the ...


4

First criticism -- I feel like those unit tests do a very bad job of documenting the intent of the code. @Test void testUserDocumentDataIsCorrect() { assert testUserDocumentBuilder.getUserName() == USERNAME assert testUserDocumentBuilder.getDateCreated() instanceof Date } At the surface level, it's hard to recognize what is being tested here, ...


4

I'll start from what I perceive as "top" and work my way down. <div> <form action="/api/blogPosts" method="post"> <input onChange={this.handleChange} type="text" name="postbody"></input> <button type="button" onClick={this.handleClick}>Submit</button> </form> <List array={this.state.posts} /> <...


4

Utilize the query object, this type of querying is more suitable there than as params. Use object destructuring. Dry your code, see how we're mapping over [countryId, stateId] to check the validity of the input. Ideally, you would have been using a module like Joi for that. Maintain a happy path and break out of the function early when erroring. Use async/...


3

So some minor comments to kick things off: Your method names are not PEP8 compliant. Usually camel case is avoided in Python. Names like checkArgumentsLength should really be more along the lines of check_arguments_length. It is bad practice to put sys.argv reads in the middle of your classes. Imagine a case where someone imports your code from Ipython, or ...


3

Interesting that you're getting .finally() after your first method rather than at the end. Should I read that to mean that your code executed as: connect-->finally-->[DONE] or did control flow somewhere else in the promise chain subsequent to finally? Regarding the aesthetics - I couldn't agree with your more; ninvoke and its relatives are not the easiest ...


3

Overall, it looks pretty good. Here are some points to consider: There is no need to explicitly return at the end of void methods, for example, at the end of displayAll(). Keep your variable declarations close to where they are used. For example, in insertNumber(), I would change this... String stringName = ""; String stringNumber = ""; System.out.println(...


3

You should only open one connection to mongodb. Otherwise if you have 10 users connected on your chat, you'll have 10 opened connections to mongodb. var mongo = require('mongodb').MongoClient; var sio = require('socket.io'); mongo.connect('mongodb://127.0.0.1/chat', function(err, db) { if(err) throw err; var messages = db.collection('messages'); ...


3

I haven't used MongoDB, but I have a few suggestions based on experiences with optimization and other databases. Index hashes of the URLs and search for those instead. Using a simple MD5 hash would probably speed up searching with the cost of dealing with false positives (unlikely but possible). Store every URL as a top-level object and add a redirectsTo ...


3

Fun code, there are a few Mocha / Jasmine submissions on this site, and this one is by far the easiest to follow. Which is all kinds of funny since this is the first time you are writing tests ;) For your questions: Your code looks fine, it seems that Mocha and should.js play very nice together and naturally drive you to grokkable code. I did not find ...


Only top voted, non community-wiki answers of a minimum length are eligible