Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB find() returns nothing

I am trying to query my database from the mongodb shell to retrieve all the entries. Still, my find() method returns nothing.

This is the mongoose model that I am using:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var ArticleSchema = new Schema({
    title: String,
    content: String,
    author: { type: String, default: 'Vlad'},
    postDate: { type: Date, default: Date.now }
});

ArticleSchema.methods.formatTitle = function() {
    var link = this.title.replace(/\s/g, '-');
    return '/article/' + link;
};

ArticleSchema.methods.snapshot = function() {
    var snapshot = this.content.substring(0, 500);
    return snapshot;
};

module.exports = mongoose.model('Article', ArticleSchema);

When loading my Express.js application in the browser, I have no issues displaying all the articles that I have added using my API. Still, I just want to go inside the mongo shell and query them. I have used the following query:

// blog is the database name (mongodb://localhost:27017/blog)
db.blog.find()

I get an empty string in return, basically a new line in the console.

like image 857
vladzam Avatar asked Sep 20 '13 09:09

vladzam


3 Answers

Your answer is right there, in the question:

// blog is the database name (mongodb://localhost:27017/blog)
db.blog.find()

So db already refers to correct database. Instead of blog, put collection name, not db name.

db.articles.find()
like image 80
Sergio Tulentsev Avatar answered Oct 03 '22 15:10

Sergio Tulentsev


Mongo shell will connect when started to the test database, you will need to change the database and then execute the find on your collection :

use blog
db.articles.find()
like image 33
richardtz Avatar answered Oct 03 '22 16:10

richardtz


First you choose the db, then you find on the collection.

use blog
db.articles.find()
like image 32
Andreas Hultgren Avatar answered Oct 03 '22 14:10

Andreas Hultgren