Delete everything in a MongoDB database
In the mongo shell: use [database]; db.dropDatabase(); And to remove the users: db.dropAllUsers();
In the mongo shell: use [database]; db.dropDatabase(); And to remove the users: db.dropAllUsers();
In NoSQL: If Only It Was That Easy, the author writes about MongoDB: MongoDB is not a key/value store, it’s quite a bit more. It’s definitely not a RDBMS either. I haven’t used MongoDB in production, but I have used it a little building a test app and it is a very cool piece of … Read more
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
You created the directory in the wrong place /data/db means that it’s directly under the “https://stackoverflow.com/” root directory, whereas you created ‘data/db’ (without the leading /) probably just inside another directory, such as the ‘/root’ homedirectory. You need to create this directory as root Either you need to use sudo , e.g. sudo mkdir -p … Read more
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.
(note: this is answer to original version of the question, which did not have requirements for “default”) You can ask it to be pretty. db.collection.find().pretty()
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
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
This is more an art than a science. The Mongo Documentation on Schemas is a good reference, but here are some things to consider: Put as much in as possible The joy of a Document database is that it eliminates lots of Joins. Your first instinct should be to place as much in a single … Read more
You can do it with the following code. db.users.findOne({“username” : {$regex : “son”}});