Node.js 在mongoose中使用集合而不是模型查找值
我试图在没有定义模型的情况下通过mongoose查询MongoDB NoSQLBooster中的此查询将生成正确的结果Node.js 在mongoose中使用集合而不是模型查找值,node.js,mongoose,Node.js,Mongoose,我试图在没有定义模型的情况下通过mongoose查询MongoDB NoSQLBooster中的此查询将生成正确的结果 db.coins.find({ "serverTime" : { $gt : "1523441870", $lt : "1523441999"}}); -- 下面的find()将返回数据库中的信息。但它不会从数据库返回预期的文档。它不会返回任何结果 const db = myDB = mongoose.connect(keys.mongoURI, (err, db) =&g
db.coins.find({ "serverTime" : { $gt : "1523441870", $lt : "1523441999"}});
--
下面的find()将返回数据库中的信息。但它不会从数据库返回预期的文档。它不会返回任何结果
const db = myDB = mongoose.connect(keys.mongoURI, (err, db) => {
myDB = db;
});
db
.then(db => {
connection = mongoose.connection;
console.log('Mongodb has been connected', myDB);
//connection.collection("coins").find({ "serverTime" : { $gt : "1523441870", $lt : "1523441999"}}, function (err, info) {
connection.collection("coins").find({}, function (err, info) {
console.log("info: ", err);
console.log("info: ", info);
});
})
.catch(err => {
console.log('Error while trying to connect with mongodb');
throw err;
});
Cursor{pool:null,server:null,disconnectHandler:Store,bson:bson,ns:“dev.coins”,…}
index.js:29
_事件:对象{}
_事件提示:0
_maxListeners:未定义
_readableState:readableState{objectMode:true,highWaterMark:16,buffer:BufferList,…}
bson:bson{}
cmd:Object{find:“dev.coins”,限制:0,跳过:0,…}
cursorState:Object{cursorId:null,cmd:Object,documents:Array(0),…}
被毁:假
disconnectHandler:存储{s:Object,长度:}
域:空
记录器:记录器{className:“游标”}
名称空间:对象
ns:“开发币”
选项:对象{readPreference:readPreference,跳过:0,限制:0,…}
池:空
可读性:正确
readPreference:readPreference
对象{numberOfRetries:5,tailableRetryInterval:500,currentNumberOfRetries:5,…}
服务器:空
sortValue:未定义
拓扑:服务器{domain:null,_events:Object,_eventscont:23,…}
__proto_uuu:可读{setCursorBatchSize:,cursorBatchSize:,setCursorLimit:,…}
首先获取收藏。
照顾好toArray,然后承诺你要寻找的信息。
使用返回的信息循环或执行所需操作
const db = myDB = mongoose.connect(keys.mongoURI, (err, db) => {
myDB = db;
});
db
.then(db => {
connection = mongoose.connection;
let collection = mongoose.connection.db.collection('coins');
console.log('Mongodb has been connected', myDB);
//Find the information
collection.find({ "serverTime" : { $gt : "1523441870", $lt : "1523441999"}}, function (err, info) {
//collection.find({}, function (err, info) {
console.log("info: ", err);
console.log("info: ", info);
//Convert to Array.
//The toArray will return a promise with the info you are looking for.
let array = info.toArray().then(info => {
console.log("more info: ", info[0]["serverTime"]);
});
console.log("info: ", array);
});
})
.catch(err => {
console.log('Error while trying to connect with mongodb');
throw err;
});
在mongoose.connection.collection上调用方法时,实际上是在访问mongoose依赖的mongodb提供的本机驱动程序 在此上下文中,返回一个包含的,它接受回调或返回承诺 您只需将回调从
.find()
调用移动到.toArray()
调用,如下所示:
connection.collection("coins").find({}).toArray(function (err, info) {
console.log("info: ", err);
console.log("info: ", info);
})
let results = connection.collection("coins").find({}).toArray()
results.then(console.log).catch(console.error)
或者,您可以实施基于承诺的解决方案,如下所示:
connection.collection("coins").find({}).toArray(function (err, info) {
console.log("info: ", err);
console.log("info: ", info);
})
let results = connection.collection("coins").find({}).toArray()
results.then(console.log).catch(console.error)
请记住,生成的数组将包含整个集合。随着时间的推移,随着文档数量及其大小的增加,这可能会导致node.js耗尽堆空间并崩溃。info是一个对象。您需要对它调用
toArray(callback)
,以将所有结果作为一个数组。太棒了!但我如何继续往下钻呢?承诺{pending}index.js:32[[PromiseStatus]]:“已解决”[[PromiseValue]]:数组(3)长度:3原型:数组(0)[,…]0:Object{id:ObjectID,serverTime:“1523441876”,BTC:Object,…}1:Object{id:ObjectID,serverTime:“1523441882”,BTC:Object,}2:Object{id:ObjectID:ObjectID,serverTime:“1523441888”,BTC:Object,}toArray()
返回一个值。了解更多信息并重新访问,它将更加清晰。