Questions tagged [mongodb]
MongoDB is a scalable, high-performance, open source, document-oriented NoSQL database. It supports a large number of languages and application development platforms. Questions about server administration can be asked on https://dba.stackexchange.com.
171,296
questions
1833
votes
45
answers
1.6m
views
How to query MongoDB with "like"
I want to query something with SQL's like query:
SELECT * FROM users WHERE name LIKE '%m%'
How can I achieve the same in MongoDB? I can't find an operator for like in the documentation.
1174
votes
16
answers
347k
views
"Large data" workflows using pandas [closed]
I have tried to puzzle out an answer to this question for many months while learning pandas. I use SAS for my day-to-day work and it is great for it's out-of-core support. However, SAS is horrible ...
921
votes
22
answers
499k
views
How do I drop a MongoDB database from the command line?
What's the easiest way to do this from my bash prompt?
897
votes
13
answers
658k
views
Query for documents where array size is greater than 1
I have a MongoDB collection with documents in the following format:
{
"_id" : ObjectId("4e8ae86d08101908e1000001"),
"name" : ["Name"],
"zipcode" : ["2223"]
}
{
"_id" : ObjectId("...
874
votes
23
answers
904k
views
How can I list all collections in the MongoDB shell?
In the MongoDB shell, how do I list all collections for the current database that I'm using?
762
votes
6
answers
314k
views
MongoDB vs. Cassandra [closed]
I am evaluating what might be the best migration option.
Currently, I am on a sharded MySQL (horizontal partition), with most of my data stored in JSON blobs. I do not have any complex SQL queries (...
713
votes
13
answers
571k
views
Find MongoDB records where array field is not empty
All of my records have a field called "pictures". This field is an array of strings.
I now want the newest 10 records where this array IS NOT empty.
I've googled around, but strangely enough I haven'...
682
votes
7
answers
205k
views
When to use CouchDB over MongoDB and vice versa
I am stuck between these two NoSQL databases.
In my project, I will be creating a database within a database. For example, I need a solution to create dynamic tables.
So users can create tables with ...
681
votes
15
answers
740k
views
Checking if a field contains a string
I'm looking for an operator, which allows me to check, if the value of a field contains a certain string.
Something like:
db.users.findOne({$contains:{"username":"son"}})
Is that possible?
667
votes
13
answers
721k
views
Find document with array that contains a specific value
If I have this schema...
person = {
name : String,
favoriteFoods : Array
}
... where the favoriteFoods array is populated with strings. How can I find all persons that have "sushi" as their ...
662
votes
16
answers
698k
views
How to get the last N records in mongodb?
I can't find anywhere it has been documented this. By default, the find() operation will get the records from beginning. How can I get the last N records in mongodb?
Edit: also I want the returned ...
625
votes
10
answers
231k
views
MongoDB relationships: embed or reference?
I want to design a question structure with some comments. Which relationship should I use for comments: embed or reference?
A question with some comments, like stackoverflow, would have a structure ...
619
votes
11
answers
749k
views
How do you query for "is not null" in Mongo?
I would like to execute a following query:
db.mycollection.find(HAS IMAGE URL)
What should be the correct syntax?
610
votes
19
answers
522k
views
How do I perform the SQL Join equivalent in MongoDB?
How do I perform the SQL Join equivalent in MongoDB?
For example say you have two collections (users and comments) and I want to pull all the comments with pid=444 along with the user info for each.
...
572
votes
12
answers
257k
views
How do you rename a MongoDB database?
There's a typo in my MongoDB database name and I'm looking to rename the database.
I can copy and delete like so...
db.copyDatabase('old_name', 'new_name');
use old_name
db.dropDatabase();
Is there ...
571
votes
8
answers
161k
views
Pretty print in MongoDB shell as default
Is there a way to tell Mongo to pretty print output? Currently, everything is output to a single line and it's difficult to read, especially with nested arrays and documents.
545
votes
29
answers
478k
views
Mongod complains that there is no /data/db folder
I am using my new mac for the first time today. I am following the get started guide on the mongodb.org up until the step where one creates the /data/db directory. btw, I used the homebrew route.
So ...
535
votes
17
answers
974k
views
Find objects between two dates MongoDB
I've been playing around storing tweets inside mongodb, each object looks like this:
{
"_id" : ObjectId("4c02c58de500fe1be1000005"),
"contributors" : null,
"text" : "Hello world",
"user" : {
"...
529
votes
10
answers
126k
views
When to use MongoDB or other document oriented database systems? [closed]
We offer a platform for video- and audio-clips, photos and vector-grafics. We started with MySQL as the database backend and recently included MongoDB for storing all meta-information of the files, ...
521
votes
17
answers
352k
views
Delete everything in a MongoDB database
I'm doing development on MongoDB. For totally non-evil purposes, I sometimes want to blow away everything in a database—that is, to delete every single collection, and whatever else might be lying ...
518
votes
12
answers
338k
views
Update MongoDB field using value of another field
In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like:
UPDATE Person SET Name = FirstName + ' ' + LastName
And the ...
488
votes
23
answers
664k
views
How do I update/upsert a document in Mongoose?
Perhaps it's the time, perhaps it's me drowning in sparse documentation and not being able to wrap my head around the concept of updating in Mongoose :)
Here's the deal:
I have a contact schema and ...
485
votes
19
answers
132k
views
MongoDB or CouchDB - fit for production? [closed]
I was wondering if anyone can tell me if MongoDB or CouchDB are ready for a production environment.
I'm now looking at these storage solutions (I'm favouring MongoDB at the moment), however these ...
479
votes
22
answers
473k
views
How to execute mongo commands through shell scripts?
I want to execute mongo commands in shell script, e.g. in a script test.sh:
#!/bin/sh
mongo myDbName
db.mycollection.findOne()
show collections
When I execute this script via ./test.sh, then the ...
470
votes
18
answers
447k
views
Retrieve only the queried element in an object array in MongoDB collection
Suppose you have the following documents in my collection:
{
"_id":ObjectId("562e7c594c12942f08fe4192"),
"shapes":[
{
"shape":"square",
"color":"blue"
},
...
465
votes
10
answers
324k
views
When to Redis? When to MongoDB? [closed]
What I want is not a comparison between Redis and MongoDB. I know they are different; the performance and the API is totally different.
Redis is very fast, but the API is very 'atomic'. MongoDB will ...
448
votes
7
answers
503k
views
Add new field to every document in a MongoDB collection
How can I add a new field to every document in an existent collection?
I know how to update an existing document's field but not how to add a new field to every document in a collection. How can I do ...
445
votes
18
answers
402k
views
How to remove a field completely from a MongoDB document?
{
name: 'book',
tags: {
words: ['abc','123'],
lat: 33,
long: 22
}
}
Suppose this is a document. How do I remove "words" completely from all the documents in this ...
440
votes
34
answers
431k
views
How to export all collections in MongoDB?
I want to export all collections in MongoDB by the command:
mongoexport -d dbname -o Mongo.json
The result is:
No collection specified!
The manual says, if you don't specify a collection, all ...
433
votes
19
answers
574k
views
How to secure MongoDB with username and password
I want to set up user name & password authentication for my MongoDB instance, so that any remote access will ask for the user name & password. I tried the tutorial from the MongoDB site and ...
427
votes
30
answers
204k
views
How can I get a random record from MongoDB?
I am looking to get a random record from a huge collection (100 million records).
What is the fastest and most efficient way to do so?
The data is already there and there are no field in which I can ...
418
votes
8
answers
220k
views
What is the "__v" field in Mongoose
I'm using Mongoose version 3 with MongoDB version 2.2. I've noticed a __v field has started appearing in my MongoDB documents. Is it something to do with versioning? How is it used?
404
votes
16
answers
423k
views
Mongoose: findOneAndUpdate doesn't return updated document
Below is my code
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Cat = mongoose.model('Cat', {
name: String,
age: {type: Number, default: 20},
...
388
votes
12
answers
642k
views
How do I search for an object by its ObjectId in the mongo console?
I've found this question answered for C# and Perl, but not in the native interface. I thought this would work:
db.theColl.find( { _id: ObjectId("4ecbe7f9e8c1c9092c000027") } )
The query returned no ...
384
votes
26
answers
410k
views
MongoDB: Is it possible to make a case-insensitive query?
Example:
> db.stuff.save({"foo":"bar"});
> db.stuff.find({"foo":"bar"}).count();
1
> db.stuff.find({"foo":"BAR"}).count();
0
370
votes
9
answers
372k
views
MySQL vs MongoDB 1000 reads
I have been very excited about MongoDb and have been testing it lately. I had a table called posts in MySQL with about 20 million records indexed only on a field called 'id'.
I wanted to compare ...
370
votes
24
answers
260k
views
Get names of all keys in the collection
I'd like to get the names of all the keys in a MongoDB collection.
For example, from this:
db.things.insert( { type : ['dog', 'cat'] } );
db.things.insert( { egg : ['cat'] } );
db.things.insert( { ...
355
votes
24
answers
577k
views
How to select a single field for all documents in a MongoDB collection?
In my MongoDB, I have a student collection with 10 records having fields name and roll. One record of this collection is:
{
"_id" : ObjectId("53d9feff55d6b4dd1171dd9e"),
"name" : "Swati",
...
349
votes
13
answers
150k
views
How do I manage MongoDB connections in a Node.js web application?
I'm using the node-mongodb-native driver with MongoDB to write a website.
I have some questions about how to manage connections:
Is it enough using only one MongoDB connection for all requests? Are ...
345
votes
9
answers
296k
views
mongodb/mongoose findMany - find all documents with IDs listed in array
I have an array of _ids and I want to get all docs accordingly, what's the best way to do it ?
Something like ...
// doesn't work ... of course ...
model.find({
'_id' : [
'...
341
votes
24
answers
535k
views
How do I remove documents using Node.js Mongoose?
FBFriendModel.find({
id: 333
}, function (err, docs) {
docs.remove(); //Remove all the documents that match!
});
The above doesn't seem to work. The records are still there.
Can someone fix?...
341
votes
22
answers
507k
views
E11000 duplicate key error index in mongodb mongoose
Following is my user schema in user.js model -
var userSchema = new mongoose.Schema({
local: {
name: { type: String },
email : { type: String, require: true, unique: true },
...
335
votes
12
answers
288k
views
How to analyze disk usage of a Docker container
I can see that Docker takes 12GB of my filesystem:
2.7G /var/lib/docker/vfs/dir
2.7G /var/lib/docker/vfs
2.8G /var/lib/docker/devicemapper/mnt
6.3G /var/lib/docker/devicemapper/...
329
votes
8
answers
147k
views
Ways to implement data versioning in MongoDB
Can you share your thoughts how would you implement data versioning in MongoDB. (I've asked similar question regarding Cassandra. If you have any thoughts which db is better for that please share)
...
316
votes
31
answers
522k
views
Render basic HTML view?
I have a basic Node.js app that I am trying to get off the ground using the Express framework. I have a views folder where I have an index.html file. But I receive the following error when loading the ...
316
votes
9
answers
425k
views
MongoDB SELECT COUNT GROUP BY
I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I ...
314
votes
7
answers
121k
views
Stop Mongoose from creating _id property for sub-document array items
If you have subdocument arrays, Mongoose automatically creates ids for each one. Example:
{
_id: "mainId"
subDocArray: [
{
_id: "unwantedId",
field: "value"
},
...
312
votes
8
answers
546k
views
return query based on date
I have a data like this in mongodb
{
"latitude" : "",
"longitude" : "",
"course" : "",
"battery" : "0",
"imei" : "0",
"altitude" : "F:3.82V",
"mcc" : "07",
"...
312
votes
4
answers
283k
views
How to search in array of object in mongodb
Suppose the mongodb document(table) 'users' is
{
_id: 1,
name: {
first: 'John',
last: 'Backus'
},
birth: new Date('Dec 03, 1924'),
death: new Date('Mar 17, 2007'),
...
310
votes
11
answers
438k
views
Push items into mongo array via mongoose
Basically I have a mongodb collection called 'people'
whose schema is as follows:
people: {
name: String,
friends: [{firstName: String, lastName: String}]
}
Now, I have a ...