最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Mongoose to list all documents in the collection? To tell if the collection is empty? - Stack Overflow

programmeradmin1浏览0评论

I'm using a MEAN stack and writing these methods in Mongoose. I'm wondering what's wrong with what I put in the Mongoose model file. I would like to use Mongoose to simply print out a list all the documents in the myModel collection.

myModel.methods.myMethod = function(cb){
  this.model("Bids").find({}, 'myField', function(err, results){
    if (err){console.log(err);return err;}
    console.log("okay");
    console.log(results);
  })
  this.save(cb);
}

Also, what is the code that I can write in Mongoose to tell if the myModel collection is empty or not?


It's better to teach a man how to fish than to give him a fish ...

So it would be extremely helpful if you can suggest what debugging tools I can install, such as an Express middleware, that can help me debug myself. Please post your debugging suggestions here.

I'm using a MEAN stack and writing these methods in Mongoose. I'm wondering what's wrong with what I put in the Mongoose model file. I would like to use Mongoose to simply print out a list all the documents in the myModel collection.

myModel.methods.myMethod = function(cb){
  this.model("Bids").find({}, 'myField', function(err, results){
    if (err){console.log(err);return err;}
    console.log("okay");
    console.log(results);
  })
  this.save(cb);
}

Also, what is the code that I can write in Mongoose to tell if the myModel collection is empty or not?


It's better to teach a man how to fish than to give him a fish ...

So it would be extremely helpful if you can suggest what debugging tools I can install, such as an Express middleware, that can help me debug myself. Please post your debugging suggestions here.

Share Improve this question edited May 23, 2017 at 11:47 CommunityBot 11 silver badge asked Oct 9, 2015 at 0:57 MelissaMelissa 1,2765 gold badges13 silver badges30 bronze badges 2
  • 2 The second argument to find() is a String? Are you sure? Looks fishy – salezica Commented Oct 9, 2015 at 0:59
  • I got it from the Mongoose docs – Melissa Commented Oct 9, 2015 at 1:00
Add a ment  | 

1 Answer 1

Reset to default 5

I'm assuming every other setup required for mongoose is correct.

At the line below, I think 'myField' is not needed.

  this.model("Bids").find({}, 'myField', function(err, results)

Here is something more from scratch, maybe it would help you to trace-back you steps:

 var mongoose = require('mongoose');

    //connection to Mongodb instance running on=======
    //local machine or anywhere=========================
    var uri = 'mongodb://localhost:27017/test';
    var connection = mongoose.createConnection(uri);


    //Define Schema==================================
    var Schema = mongoose.Schema;
    var BlogPostSchema = new Schema({
      author: { type: Schema.Types.ObjectId },
      title: String,
      body: String
    });


    //Create model===================================================
    var BlogPostModel = connection.model('BlogPost', BlogPostSchema);


    //function to insert doc into model NOTE "pass in your =======
    //callback or do away with it if you don't need one"=========
    var insertBlogPost = function (doc, callback) {
      
      //here is where or doc is converted to mongoose object
      var newblogPost = new BlogPostModel(doc); 
      
      //save to db
      newblogPost.save(function (err) {

        assert.equal(null, err);
        
        //invoke your call back if any
        callback();
        console.log("saved successfully");
      });
    };


    //function to get all BlogPosts====================================
    var getAllBlogPosts = function (callback) {

    //mongoose get all docs. I think here answers your question directly
      BlogPostModel.find(function (err, results) {
        assert.equal(null, err);
        
        //invoke callback with your mongoose returned result
        callback(results);
      });
    };


    //you can add as many functions as you need.

    //Put all of your methods in a single object interface 
    //and expose this object using module.

    var BlogPostManager = {
        insertBlogPost: insertBlogPost,
        getAllBlogPosts : getAllBlogPosts
    }


    module.exports = BlogPostManager;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论