无法在node.js中使用mongoose查询mongoDB [英] Can't query mongoDB with mongoose in node.js

查看:63
本文介绍了无法在node.js中使用mongoose查询mongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的mongoDB中有一个集合:db.co 并且只有一个文档:

Suppose I have a collection in my mongoDB: db.co and only have one document:

{ "_id" : ObjectId("50d083e32cdcf7ce065b616c"), 
  "age" : 22, 
  "friends" : [ "Tom"], 
  "location" : "NY", 
  "name" : "lee", 
  "skill" : [ "javascript", "java" ] 
 }

然后我想通过此代码在node.js中用mongoose对其进行查询:

Then I want query it in node.js with mongoose through this code:

var coSchema = mongoose.Schema( {
    age: Number,
    friends: Array, 
    location: String,
    name: String,
    skill: Array
})

var Co = mongoose.model('Co', coSchema);
function findSomeoneInCo (name) {
  Co.find({"name": name}, function (err, doc) {
    if (err) {
        console.log('find some one failed: ' + err);
        return;
    }
    console.log('find successed: ' + doc);
  })
}

findSomeoneInCo("lee");

但它什么也没给我

我的代码有什么问题?如何获得当前的查询结果?

What's problem with my code? How can I get the corrent query result?

推荐答案

在确定要使用的集合名称时,猫鼬将小写的模型名称复数.因此,在模型名称为'Co'的情况下,默认情况下它将在cos集合中查找.

Mongoose pluralizes the lower-cased model name when determining the collection name to use. So with a model name of 'Co' it's going to be looking in the cos collection by default.

要覆盖默认值并与现有的co集合对齐:

To override the default and align with your existing co collection:

var Co = mongoose.model('Co', coSchema, 'co');

这篇关于无法在node.js中使用mongoose查询mongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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