MongoDB – is DBREF necessary?

Dbref in my opinion should be avoided when work with mongodb, at least if you work with big systems that require scalability. As i know all drivers make additional request to load DBRef, so it’s not ‘join‘ within database, it is very expensive. Is there a way to reference other documents without having the somewhat … Read more

Unable to connect to mongoDB running in docker container

If you specified the correct port and still not able to connect to mongodb running in docker (like me), make sure you are using the service name (or container name) in your connection URL, e.g. mongodb://mongodb_service:27017/mydb, which is defined in your docker-compose.yml : services: mongodb_service: image: mongo I was using the hostname value and that’s … Read more

Mongodb group and sort

Inspired by this example on mongo’s website. GENERATE DUMMY DATA: > db.stack.insert({a:1,b:1,c:1,active:1}) > db.stack.insert({a:1,b:1,c:2,active:0}) > db.stack.insert({a:1,b:2,c:3,active:1}) > db.stack.insert({a:1,b:2,c:2,active:0}) > db.stack.insert({a:2,b:1,c:3,active:1}) > db.stack.insert({a:2,b:1,c:10,active:1}) > db.stack.insert({a:2,b:2,c:10,active:0}) > db.stack.insert({a:2,b:2,c:5,active:1}) MONGO QUERY: > db.stack.aggregate( … {$match:{active:1}}, … {$group:{_id:{a:”$a”, b:”$b”}, csum:{$sum:”$c”}}}, … {$sort:{“_id.a”:1}}) RESULT: {“result” : [ {“_id” : {“a” : 1,”b” : 2},”csum” : 3}, {“_id” : {“a” : … Read more

If Mongo $lookup is a left outer join, then how come it excludes non-matching documents?

This behavior isn’t related to $lookup, it’s because the default behavior for $unwind is to omit documents where the referenced field is missing or an empty array. To preserve the unwound documents even when profile.universities is an empty array, you can set its preserveNullAndEmptyArrays option to true: db.users.aggregate([ { $unwind: “$profile”, $unwind: { path: “$profile.universities”, … Read more

Multiply field by value in Mongodb

You can run server-side code with db.eval(). db.eval(function() { db.collection.find({tag : “refurb”}).forEach(function(e) { e.Price = e.Price * 0.5; db.collection.save(e); }); }); Note this will block the DB, so it’s better to do find-update operation pair. See https://docs.mongodb.com/manual/core/server-side-javascript/

MongoDB: Bulk insert (Bulk.insert) vs insert multiple (insert([…]))

@Dummy is correct that bulk operations are generally faster than individual inserts, however, from version 2.6 and above, inserting multiple documents using collection.insert is just syntactic sugar for a BulkWrite. If you set the ordered flag to false, performance should be identical to an unordered bulk insert: db.collection.insert(<document array>,{ordered:false}) This operation will return a BulkWriteResult, … Read more

How to use GridFS to store images using Node.js and Mongoose

I was not satisfied with the highest rated answer here and so I’m providing a new one: I ended up using the node module ‘gridfs-stream’ (great documentation there!) which can be installed via npm. With it, and in combination with mongoose, it could look like this: var fs = require(‘fs’); var mongoose = require(“mongoose”); var … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)