本文介绍了无法在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
发布评论