![](/img/trans.png)
[英]select collection and get the last item of each in other collection in MongoDB
[英]How to get the value of the last item in a Mongo collection
我讀了一個Mongo集合,我想獲取最后一個項目的價值。 我做了一個方法,從最舊到最新( stackoverflow問題 )進行排序,僅采用第一項,但我無法獲得他的價值。
這是我在集合中插入數據的代碼:
'container.start': function(id) {
var ctn = docker.getContainer(id);
ctn.start(Meteor.bindEnvironment(function(err, data) {
if(err){
ErrorsContainer.upsert({
}, {
error: err
});
console.log(err);
}else{
console.log(data);
}
}));
}
然后是讀取集合的代碼:
'click .start'(event) {
const idDB = this._id
const container = InfosContainers.findOne({_id: idDB});
const name = container["nameContainer"];
const idContainer = container["idContainer"];
console.log("the container: " + name + " is going to be started. His id is: " + idContainer);
Meteor.call("container.start",idContainer);
//get the last error
var error = ErrorsContainer.find({}, {sort: {_id: 1, limit: 1}});
if(error){
alert(error[error]); //tried error.error too
}
}
那么,如何獲得collection的錯誤值呢?
問題是集合的錯誤包含其他內容,我必須這樣做:
var lastError = ErrorsContainer.findOne({}, {sort: {_id: 1}});
console.log(lastError.error.message);
做
{ sort: { created : -1 } }
其中created是文檔的創建時間,-1是按DEC ordre排序(1是按INC順序排序)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.