我想創建一個集合命令的集合:的MongoDB:如何創建由2場
var ethTransactionSchema = new mongoose.Schema({
blockNumber: Number,
transactionIndex: Number,
from: String,
to: String,
data: String
});
我需要通過blockNumber,transactionIndex得到有序的結果:當2項具有相同的blockNumber的transactionIndex將確定命令。
我有一個指標:
ethTransactionSchema.index({ blockNumber: 1, transactionIndex: 1 }, { unique: true });
ethTransactionSchema.index({ from: 1 });
當我執行查詢:
EthTransaction.find({ from: 'address' }).sort({ blockNumber: 1, transactionIndex: 1 }).limit(20)
和另一個查詢:
EthTransaction.find(
{
$and: [
{
$or: [ { from: 'address1' }, { to: 'address2' } ]
},
{
$or: [{ blockNumber: { $gt: lastBlockGot } }, { $and: [{ blockNumber: lastBlockGot }, { transactionIndex: { $gt: lastIndexGot } }] }]
}]
}
).sort({ blockNumber: 1, transactionIndex: 1 }).limit(20)
我的作品完美,但速度太慢。我想知道如何插入由blockNumber,transactionIndex排序的字段,以便我可以在沒有排序子句的情況下檢索結果。
的官方文檔*我想知道如何插入blockNumber,transactionIndex排序的字段,這樣我就可以在沒有排序子句的情況下檢索結果。* - 您不能。沒有廣告插入順序。如果你想排序,你需要使用'.sort()' – cdbajorin