1

テーブルのすべてのレコードから最後の 40 レコードを取得するにはどうすればよいですか。

私の知る限り、インデックス付きデータベースのテーブルに存在するレコードの数を取得できます。

同様に...

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

上記は、テーブル内のカウントが何であれ返されます。今、テーブルから最後の40レコードを取得したかったのです。毎回テーブルには異なるレコードがあります。そうすることは可能ですか?count で.eachを使用することもできますか !! 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