1

我们如何从表的所有记录中获取最后 40 条记录。

据我所知,我们可以获得索引数据库表中退出的记录数。

对于相同的...

function doCount(){
        db.friends.toCollection().count(function (count) {
        console.log(count + " friends in total");
    });

上面将返回表中的任何计数。现在我想从表中获取最后 40 条记录。每个时间表都会有不同的记录。有可能这样做吗?我们是否也可以将.each 与 count 一起使用!我想获取 count 以及密钥持有的任何数据,我需要返回这些数据并显示它。

 var db = new Dexie("MyDB");
        db.version(1).stores({
            friends: "id,name,shoeSize"
        });
        db.open();
        //
        // Populate some data
        //
        function populateSomeData() {
            log("Populating some data", "heading");
            return db.transaction("rw", db.friends, function () {
                db.friends.clear();
                db.friends.add({ id:1,name: "David", shoeSize: 43 });
                db.friends.add({ id:2,name: "Ylva", shoeSize: 37 });
                db.friends.add({ id:3,name: "Jon", shoeSize: 44 });
                db.friends.add({id:4, name: "Måns", shoeSize: 42 });
                db.friends.add({ id:5,name: "Daniel", shoeSize: 39 });
                db.friends.add({ id:6,name: "Nils", shoeSize: 45 });
                db.friends.add({ id:7,name: "Zlatan", shoeSize: 47 });
                // Log data from DB:
                db.friends.orderBy('name').each(function (friend) {
                    log(JSON.stringify(friend));
                });
            }).catch(function (e) {
                log(e, "error");
            });
        }
        //
        // Examples
        //
        function equalsAnyOf() {
            log("db.friends.where('name').anyOf('David', 'Zlatan', 'Daniel')", "heading");
            return db.friends.where('name').anyOf('David', 'Zlatan', 'Daniel')
                             .each(function (friend) {
                                 log(JSON.stringify(friend));
                             });
        }
        function equalsIgnoreCase() {
            log("db.friends.where('name').equalsIgnoreCase('david')", "heading");
            return db.friends.where('name').equalsIgnoreCase('david')
                             .each(function (friend) {
                                 log(JSON.stringify(friend));
                             });
        }
        function startsWithIgnoreCase() {
            log("db.friends.where('name').startsWithIgnoreCase('da')", "heading");
            return db.friends.where('name').startsWithIgnoreCase('da')
                             .each(function (friend) {
                                 log(JSON.stringify(friend));
                             });
        }
        function logicalOR() {
            log("db.friends.where('name').startsWithIgnoreCase('da').or('shoeSize').below(40)", "heading");
            return db.friends.where('name').startsWithIgnoreCase('da')
                             .or('shoeSize').below(40)
                             .each(function (friend) {
                                 log(JSON.stringify(friend));
                             });
        }
        function logicalAND() {
            log("db.friends.where('name').startsWithIgnoreCase('da').and(function (friend) { return friend.shoeSize > 40; })", "heading");
            return db.friends.where('name').startsWithIgnoreCase('da')
                .and(function (friend) { return friend.shoeSize > 40; })
                .each(function (friend) {
                    log(JSON.stringify(friend));
                });
        }
		var lakho =[];
		function doCount(){
			db.friends.toCollection().count(function (count) {
			lakho = count;
			console.log(count + " friends in total");
			var div = document.getElementById('po');
			div.innerHTML = div.innerHTML + count;
		});
		console.log(lakho + "lakho");
		
		db.friends.where('id').between(1,3).count(function (count) {
			console.log(count + " friends in total");
			var div = document.getElementById('po');
			div.innerHTML = div.innerHTML + count;
		});
		
		db.friends
		  .where('shoeSize').above(9)
		  .each (function (friend) {
			console.log("Found big friend: " + friend.name);
		  });
}
        //
        // Helpers
        //
        function log(txt, clazz) {
            var li = document.createElement('li');
            li.textContent = txt.toString();
            if (clazz) li.className = clazz;
            document.getElementById('log').appendChild(li);
        }
        function runSamples() {
            populateSomeData()
                .then(equalsAnyOf)
                .then(equalsIgnoreCase)
                .then(startsWithIgnoreCase)
                .then(logicalOR)
                .then(logicalAND)
				.then(doCount)
            .catch(function (e) {
                log(e, "error");
            });
        }
<script src="https://unpkg.com/dexie@latest/dist/dexie.js"></script>
    <body onload="runSamples();">
    <ul id="log"></ul>
	<div id="po"> </div>
</body>

4

2 回答 2

2

我就是这样做的。

db.friends.orderBy('id')
.reverse()
.limit(3)
.toArray()
.then(function (results) {
    console.log (JSON.stringify(results));
});

参考:git hub 参考

于 2017-01-16T07:21:16.450 回答
0

您可以使用

db.yourTable
    .orderBy('id')
    .reverse()
    .limit(40)
    .each(callback) 

它将迭代最后 40 条记录。

于 2017-01-15T14:17:34.780 回答