无法在node.js中使用mongoose查询mongoDB

编程入门 行业动态 更新时间:2024-10-23 09:30:54
本文介绍了无法在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

本文发布于:2023-11-22 19:19:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618683.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js   node   mongoDB   mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!