1
我試圖插入不同的項目到一個MongoDB集合,但我想要在該數組的頂部插入項目,以便檢索收集項目,我可以得到他們排序。如何插入到一個位置的MongoDB集合
我使用這個代碼:
var json = { title : title, postid : postid, image : image, decription : description};
collection.insert(json, function (err, result) {
if (err) {
console.log(err);
} else {
console.log('Here we go', result.length, result);
}
});
而這個代碼來檢索收集物品:
collection.find().toArray(function (err, result) {
if (err) {
console.log(err);
} else if (result.length) {
first_item = result[0].postid;
console.log('Found:',first_item);
} else {
}
db.close();
});
}
});
我插入新的項目,每次10分鐘,當我取回物品我想最後插入到位置0的項目
謝謝!它完美的作品 –
@YasserB。很高興我能幫上忙。如果有幫助,請不要忘記接受答案。 –