我已在localhost上安装:
包
mongodb v3.0.4 mongoose v5.0.11
我连接到数据库并通过id
获取文档我的代码
var MongoDB = require("mongodb");
var mongo = MongoDB.MongoClient;
var mongoose = require("mongoose");
mongo.connect("mongodb://localhost:27017", function(err, client){
if (err) throw err;
var db = client.db("my_database");
db.collection("accounts").findOne({email : "email@example.com"}, function(err, doc){
console.log(doc);
});
});
结果:
{
_id : 5ab464aba4697729b8003653,
email : 'email@example.com'
}
所以,我将条件替换为{_id : new mongoose.Types.ObjectId(id)}
,我收到null
我写了其他代码:
var MongoDB = require("mongodb");
var mongo = MongoDB.MongoClient;
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/my_database');
var accSchame = mongoose.Schema({
email : {type : String}
});
var Accounts = mongoose.model('accounts', accSchame);
Accounts.findById('5ab465a8a4697729b8003655', function(err, doc){
console.log(err);
console.log(doc);
});
我也收到了null
。
有人可以告诉我为什么吗?