Fuzzy Searching with Mongodb?

I believe that to do “fuzzy” search you will need to use regex. This should accomplish what you’re looking for (escapeRegex function source here): function escapeRegex(text) { return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, “\\$&”); }; router.get(“https://stackoverflow.com/”, function(req, res) { if (req.query.search) { const regex = new RegExp(escapeRegex(req.query.search), ‘gi’); Jobs.find({ “name”: regex }, function(err, foundjobs) { if(err) { console.log(err); } … Read more

How to deal with the timezone issue when storing dates in utc using mongod?

Aside from the SERVER-6310 mentioned by Matt Johnson, one other workaround is to use the $project operator to add or subtract from the UTC time zone to “shift the time” into the correct local zone. Turns out you can add or subtract time in milliseconds. For example, assuming I have a Date field called orderTime. … Read more

How to create a Mongo Docker Image with default collections and data?

The problem was that information could not be saved on /db/data, so I’ve created a solution creating my own data directory. # Parent Dockerfile https://github.com/docker-library/mongo/blob/982328582c74dd2f0a9c8c77b84006f291f974c3/3.0/Dockerfile FROM mongo:latest # Modify child mongo to use /data/db2 as dbpath (because /data/db wont persist the build) RUN mkdir -p /data/db2 \ && echo “dbpath = /data/db2” > /etc/mongodb.conf \ … Read more

Update MongoDB collection using $toLower

MongoDB does not have a concept of $toLower as a command. The solution is to run a big for loop over the data and issue the updates individually. You can do this in any driver or from the shell: db.myCollection.find().forEach( function(e) { e.UserName = e.UserName.toLowerCase(); db.myCollection.save(e); } ) You can also replace the save with … Read more

How does the order of compound indexes matter in MongoDB performance-wise?

Redsandro, You must consider Index Cardinality and Selectivity. 1. Index Cardinality The index cardinality refers to how many possible values there are for a field. The field sex only has two possible values. It has a very low cardinality. Other fields such as names, usernames, phone numbers, emails, etc. will have a more unique value … Read more

Mongo $in operator performance

It can be fairly efficient with small lists (hard to say what small is, but at least into the tens/hundreds) for $in. It does not work like app-engine since mongodb has actual btree indexes and isn’t a column store like bigtable. With $in it will skip around in the index to find the matching documents, … Read more

How to find min value in mongodb

You can use a combination of sort and limit to emulate min: > db.foo.insert({a: 1}) > db.foo.insert({a: 2}) > db.foo.insert({a: 3}) > db.foo.find().sort({a: 1}).limit(1) { “_id” : ObjectId(“4df8d4a5957c623adae2ab7e”), “a” : 1 } sort({a: 1}) is an ascending (minimum-first) sort on the a field, and we then only return the first document, which will be the … Read more

tech