使用索引db的dexie js获取计数记录。

时间:2017-01-15 13:55:19

标签: indexeddb dexie

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

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

同样......

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

上面将返回表中的计数。现在我想从表中获取最后40条记录。每个时间表都有不同的记录。 是否有可能这样做。 我们是否也可以使用 .each 计数!!我想得到计数,以及密钥保存的任何数据,我需要返回这些数据并显示它。< / p>

 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>

2 个答案:

答案 0 :(得分:1)

这就是我这样做的方式。

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

参考:git hub ref

答案 1 :(得分:0)

您可以使用

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

它将迭代最后40条记录。