MongoDB返回undefined

我有这个简单的程序存储在MongoDB 3variables – XYMESSAGE集合命名坐标 。 现在我需要find并使用10个最新的条目。 我发现我可以用collection.find({}).skip(collection.count() - 10)来做到这一点,然后使用.toArray()将其转换为数组。 我的完整代码行看起来像这样

 var notesArray=collection.find({}).skip(collection.count() - 10).toArray; if (notesArray.length > 0) { console.log(notesArray[0]); } 

并在控制台中显示“未定义”。 我需要做的是获得一个X数组, Y数组和MESSAGE数组,它们全部包含10个最新条目? 之后我需要在其他函数中使用这10个x,y和消息。 先谢谢你。

编辑:

在将collection.find().limit(3).toArray(function(err, docs) { console.log(docs); })放入代码之后,我从数据库中得到了3条第一条logging

[{_id:5878f25df5cfe2d8002fdc72,x:450,y:265,message:'QQQQQQQQQQQQQQQQQQQQQQQQQ'},

{_id:5878f263f5cfe2d8002fdc73,x:1146,y:245,消息:'QQQQQQQQQQQQQQQQQQQQQQQQQQ'},

{_id:5878f267f5cfe2d8002fdc74,x:216,y:175,message:'QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ QQQQ'}]

我想我需要做这样的collection.find().skip(collection.count() - 10).toArray(function(err, docs) { console.log(docs); })但是我得到错误TypeError: callback is not a function

EDIT2:

一切都是通过将大写字母改为小写字母来解决的。

您可以使用聚合框架来获得所需的结果。 以下示例演示了如何在nodejs中运行aggregate()pipe道:

 // Correctly call the aggregation framework using a pipeline in an Array. var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/test', function(err, db) { // Create a collection var collection = db.collection('coordinates'); // Execute aggregate, notice the pipeline is expressed as an Array collection.aggregate([ { "$sort": { "_id": -1 } }, { "$limit": 10 }, { "$group": { "_id": null, "x": { "$push": "$x" }, "y": { "$push": "$y" }, "message": { "$push": "$message" } } } ], function(err, notesArray) { console.log('X: ', notesArray[0].x); console.log('Y: ', notesArray[0].y); console.log('MESSAGE: ', notesArray[0].message); var firstX = notesArray[0].x[0]; console.log(firstX); console.log(notesArray); db.close(); }); }); 

上面的聚合操作首先使用$sortpipe道运算符对_id字段中的文档进行$sort 。 由于在存储ObjectId值的_id字段上sorting大致等同于按创build时间进行sorting,因此如果MongoDB驱动程序在文档创build时自动为_id字段生成ObjectId ,则可以获取最新的文档。 否则,如果_id不是ObjectId ,则需要在表示文档创builddate时间的时间戳字段上进行sorting,以确定查询中最后的n个文档。

下一步需要从sorting列表中select前10个文档。 这是通过$limit操作符实现的。

最后,将所有文档分组,并通过$grouppipe道为每个字段创build数据列表。 $push累加器运算符可以在指定的字段上从组中创build一个值的数组。 $group阶段中的_id字段是强制性的; 但是,您可以指定一个_id值为null来计算所有input文档的累积值,如上所述。

最后的结果是只有一个单一文件的数组,它有一个_idnull ,三个字段,即X数组, Y数组和MESSAGE数组,它们都包含10个最新条目:

 notesArray = [ { "_id": null, "X": [/* last 10 x values */], "Y": [/* last 10 y values */], "MESSAGE": [/* last 10 MESSAGE values */] } ] 

这应该做的伎俩。 Mongo答应答应你必须调用then函数来访问数据。

 var url = 'mongodb://127.0.0.1:27017/my_db_name'; MongoClient.connect(url, function(err, db){ if(!err){ var collection = db.collection('coordinates'); collection.find({},{_id : 0}, {limit : 10}).toArray().then(function(results){ console.log(results); }).catch(function(err){ console.log(err); }); } }) 

你需要一个callback。

 collection .find() .limit(10) .toArray(function(err, notesArray) { if (notesArray.length > 0) console.log(notesArray[0]); }); 

有关MongoDB文档的更多信息。

编辑包括数据库连接和集合名称

以下是您需要的完整代码:

 var MongoClient = require('mongodb').MongoClient; // Change the database URL and database name as per your requirement MongoClient.connect("mongodb://localhost:27017/database_name", function(err, db) { if(err) return console.dir(err); var collection = db.collection('coordinates'); collection .find() .limit(10) .toArray(function(err, notesArray) { if (notesArray.length > 0) console.log(notesArray[0]); }); });