如何让Mongoose列出集合中的所有文档?要判断集合是否为空? [英] How to get Mongoose to list all documents in the collection? To tell if the collection is empty?

查看:109
本文介绍了如何让Mongoose列出集合中的所有文档?要判断集合是否为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MEAN堆栈并在Mongoose中编写这些方法。我想知道我放在Mongoose模型文件中的内容有什么问题。我想使用Mongoose简单地打印myModel集合中所有文档的列表。

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);
}

此外,我可以在Mongoose中编写什么代码来判断myModel集合是否为空?

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 ...

因此,如果您可以建议我可以安装哪些调试工具(例如Express中间件)可以帮助我自我调试,那将非常有用。 请在此处发布您的调试建议

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.

推荐答案

我假设mongoose所需的所有其他设置都是正确的。

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

在下面的行中,我认为不需要'myField'。

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;

这篇关于如何让Mongoose列出集合中的所有文档?要判断集合是否为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆