indexeddb - 使用 dexie js 和索引数据库获取计数记录。

标签 indexeddb dexie

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

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

对于同样的...

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

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

 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>

最佳答案

我就是这样做的。

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

引用号:git hub ref

关于indexeddb - 使用 dexie js 和索引数据库获取计数记录。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661892/

相关文章:

google-chrome - IndexedDB:复制到不同的域?

javascript - 德克西JS : very large IndexedDB but empty tables

javascript - Reactjs 从 Dexie.js 中删除一个项目

javascript - 在 Dexie 数据库模式中指定不同的唯一键?

javascript - DexiestartsWithIgnoreCase() 函数数组未定义

html - 检查 IndexedDB 数据库是否存在

javascript - indexedDb 打开时没有事件触发

json - 如何从数组中获取特定值?

javascript - IndexedDB 为简单对象占用大量空间

javascript - 使用 Dexie.js 编写多个 where 条件