我无法从 mongodb 中检索值,即使它存在
I'm not able to retrieve value from mongodb even though it exists
我在本地 运行 mongo。在这里,我将条目插入到我的数据库中并验证它是否存在
MongoDB shell version: 3.2.9
connecting to: test
> use testing
switched to db testing
> db.vetschool.insert({name: 'My First Cat'})
WriteResult({ "nInserted" : 1 })
> db.vetschool.findOne()
{ "_id" : ObjectId("57c051cbd7bd69709fa7d98a"), "name" : "My First Cat" }
在这里,我定义了我的模式并执行了 findOne() 函数,该函数 returns 一个空值。虽然未显示,但我确实需要 mongoose 在我的文档顶部。
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Successfully connected to MongoDB');
});
var kittySchema = mongoose.Schema({
name: String
});
var Kitten = mongoose.model('Kitten', kittySchema);
Kitten.findOne().exec(function(err, success){
console.log(success); //returns null
console.log(err); //returns null
})
这可能是太多的附加信息,但这是我启动服务器时的输出以及输出的空值
[nodemon] starting `node server.js`
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
Listening on port 3000...
Successfully connected to MongoDB
null
null
简短回答:您正在使用 mongo 手动将文档插入 vetschool
集合,然后要求 Mongoose 在 Kittens
集合中查找该文档。
长答案:Mongoose 会根据您传递给它的模式名称自动确定集合的名称。来自 http://mongoosejs.com/docs/models.html :
The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.
因此,如果您通过 Mongoose 执行某些操作并手动执行某些操作,则需要您确保集合名称匹配。
我在本地 运行 mongo。在这里,我将条目插入到我的数据库中并验证它是否存在
MongoDB shell version: 3.2.9
connecting to: test
> use testing
switched to db testing
> db.vetschool.insert({name: 'My First Cat'})
WriteResult({ "nInserted" : 1 })
> db.vetschool.findOne()
{ "_id" : ObjectId("57c051cbd7bd69709fa7d98a"), "name" : "My First Cat" }
在这里,我定义了我的模式并执行了 findOne() 函数,该函数 returns 一个空值。虽然未显示,但我确实需要 mongoose 在我的文档顶部。
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Successfully connected to MongoDB');
});
var kittySchema = mongoose.Schema({
name: String
});
var Kitten = mongoose.model('Kitten', kittySchema);
Kitten.findOne().exec(function(err, success){
console.log(success); //returns null
console.log(err); //returns null
})
这可能是太多的附加信息,但这是我启动服务器时的输出以及输出的空值
[nodemon] starting `node server.js`
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
Listening on port 3000...
Successfully connected to MongoDB
null
null
简短回答:您正在使用 mongo 手动将文档插入 vetschool
集合,然后要求 Mongoose 在 Kittens
集合中查找该文档。
长答案:Mongoose 会根据您传递给它的模式名称自动确定集合的名称。来自 http://mongoosejs.com/docs/models.html :
The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.
因此,如果您通过 Mongoose 执行某些操作并手动执行某些操作,则需要您确保集合名称匹配。