Questions tagged [mongodb]
MongoDB is a scalable, cross-platform, 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
175,975
questions
1934
votes
47
answers
1.8m
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.
1183
votes
16
answers
353k
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 ...
971
votes
16
answers
775k
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("...
935
votes
22
answers
518k
views
How do I drop a MongoDB database from the command line?
What's the easiest way to do this from my bash prompt?
910
votes
23
answers
1.0m
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?
774
votes
13
answers
679k
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'...
764
votes
6
answers
316k
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 (...
746
votes
17
answers
896k
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?
716
votes
14
answers
836k
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 ...
692
votes
7
answers
209k
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 ...
691
votes
16
answers
762k
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 ...
684
votes
11
answers
896k
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?
640
votes
19
answers
571k
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.
...
635
votes
10
answers
244k
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 ...
595
votes
12
answers
297k
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 ...
574
votes
8
answers
168k
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.
558
votes
17
answers
1.1m
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" : {
"...
548
votes
28
answers
488k
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 ...
544
votes
12
answers
392k
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 ...
538
votes
17
answers
383k
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 ...
531
votes
10
answers
127k
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, ...
509
votes
23
answers
714k
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 ...
494
votes
22
answers
517k
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 ...
485
votes
20
answers
489k
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"
},
...
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 ...
477
votes
18
answers
473k
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 ...
473
votes
8
answers
571k
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 ...
467
votes
34
answers
497k
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 ...
466
votes
10
answers
331k
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 ...
445
votes
30
answers
227k
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 ...
444
votes
20
answers
633k
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 ...
436
votes
9
answers
253k
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?
431
votes
17
answers
464k
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},
...
406
votes
13
answers
733k
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 ...
400
votes
27
answers
460k
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
397
votes
25
answers
688k
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",
...
377
votes
9
answers
388k
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 ...
377
votes
25
answers
278k
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( { ...
367
votes
24
answers
595k
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 },
...
362
votes
13
answers
158k
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 ...
361
votes
9
answers
333k
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' : [
'...
349
votes
24
answers
574k
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?...
348
votes
9
answers
510k
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 ...
345
votes
8
answers
514k
views
How to list all databases in the mongo shell?
I know how to list all collections in a particular database, but how do I list all available databases in MongoDB shell?
340
votes
12
answers
625k
views
MongoDB - 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",
"...
337
votes
4
answers
336k
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'),
...
337
votes
7
answers
158k
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)
...
331
votes
8
answers
141k
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"
},
...
331
votes
12
answers
493k
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 ...
329
votes
17
answers
532k
views
MongoDB - admin user not authorized
I am trying to add authorization to my MongoDB.
I am doing all this on Linux with MongoDB 2.6.1.
My mongod.conf file is in the old compatibility format
(this is how it came with the installation).
...