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
mongodb
176,128
questions
1949
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.
1184
votes
16
answers
354k
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 ...
978
votes
16
answers
792k
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("...
939
votes
22
answers
523k
views
How do I drop a MongoDB database from the command line?
What's the easiest way to do this from my bash prompt?
915
votes
23
answers
1.1m
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?
779
votes
13
answers
696k
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 (...
749
votes
17
answers
918k
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?
722
votes
14
answers
855k
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 ...
694
votes
16
answers
770k
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 ...
692
votes
7
answers
210k
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 ...
686
votes
11
answers
913k
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?
641
votes
19
answers
579k
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.
...
638
votes
10
answers
246k
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 ...
601
votes
13
answers
304k
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 ...
576
votes
8
answers
169k
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.
561
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" : {
"...
550
votes
29
answers
490k
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 ...
546
votes
12
answers
399k
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 ...
539
votes
17
answers
388k
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 ...
530
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, ...
510
votes
23
answers
720k
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 ...
496
votes
22
answers
523k
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 ...
487
votes
18
answers
486k
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 ...
485
votes
20
answers
492k
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 ...
475
votes
8
answers
579k
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 ...
473
votes
34
answers
508k
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
332k
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
30
answers
230k
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 ...
446
votes
20
answers
643k
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 ...
438
votes
9
answers
258k
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?
432
votes
17
answers
469k
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},
...
409
votes
13
answers
750k
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 ...
405
votes
27
answers
468k
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
405
votes
25
answers
706k
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",
...
382
votes
9
answers
392k
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 ...
378
votes
26
answers
281k
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( { ...
371
votes
24
answers
610k
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
9
answers
337k
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' : [
'...
361
votes
13
answers
159k
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 ...
353
votes
9
answers
522k
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 ...
350
votes
8
answers
531k
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?
348
votes
24
answers
578k
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?...
344
votes
12
answers
643k
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",
"...
339
votes
4
answers
340k
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
8
answers
143k
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"
},
...
337
votes
7
answers
159k
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
12
answers
500k
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 ...
330
votes
17
answers
543k
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).
...