Find objects between two dates MongoDB

Querying for a Date Range (Specific Month or Day) in the MongoDB Cookbook has a very good explanation on the matter, but below is something I tried out myself and it seems to work. items.save({ name: “example”, created_at: ISODate(“2010-04-30T00:00:00.000Z”) }) items.find({ created_at: { $gte: ISODate(“2010-04-29T00:00:00.000Z”), $lt: ISODate(“2010-05-01T00:00:00.000Z”) } }) => { “_id” : ObjectId(“4c0791e2b9ec877893f3363b”), “name” … Read more

How do you rename a MongoDB database?

You could do this, if you’re using MongoDB < 4.2 (ref): db.copyDatabase(“db_to_rename”,”db_renamed”,”localhost”) use db_to_rename db.dropDatabase(); Editorial Note: this is the same approach used in the question itself but has proven useful to others regardless.

How do I perform the SQL Join equivalent in MongoDB?

As of Mongo 3.2 the answers to this question are mostly no longer correct. The new $lookup operator added to the aggregation pipeline is essentially identical to a left outer join: https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._S_lookup From the docs: { $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the … Read more

How do you query for “is not null” in Mongo?

This will return all documents with a key called “IMAGE URL”, but they may still have a null value. db.mycollection.find({“IMAGE URL”:{$exists:true}}); This will return all documents with both a key called “IMAGE URL” and a non-null value. db.mycollection.find({“IMAGE URL”:{$ne:null}}); Also, according to the docs, $exists currently can’t use an index, but $ne can. Edit: Adding … Read more

Find document with array that contains a specific value

As favouriteFoods is a simple array of strings, you can just query that field directly: PersonModel.find({ favouriteFoods: “sushi” }, …); // favouriteFoods contains “sushi” But I’d also recommend making the string array explicit in your schema: person = { name : String, favouriteFoods : [String] } The relevant documentation can be found here: https://docs.mongodb.com/manual/tutorial/query-arrays/

How to get the last N records in mongodb?

If I understand your question, you need to sort in ascending order. Assuming you have some id or date field called “x” you would do … .sort() db.foo.find().sort({x:1}); The 1 will sort ascending (oldest to newest) and -1 will sort descending (newest to oldest.) If you use the auto created _id field it has a … Read more

When to use CouchDB over MongoDB and vice versa

Of C, A & P (Consistency, Availability & Partition tolerance) which 2 are more important to you? Quick reference, the Visual Guide To NoSQL Systems MongodB : Consistency and Partition Tolerance CouchDB : Availability and Partition Tolerance A blog post, Cassandra vs MongoDB vs CouchDB vs Redis vs Riak vs HBase vs Membase vs Neo4j … Read more

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