4

I want to display first 20 records from collection on one Jframe and next 20 on another frame . I am newbie to MongoDB. please suggest a query to find first 20 and next 20 documents.

4

3 回答 3

13

在 MongoDB shell 上,您可以执行以下操作:

db.collectionName.find( { city: "London" } ).skip( 20 ).limit( 20 );

显示文档 21 到 40 的结果。

请查看限制并跳过:http ://docs.mongodb.org/manual/core/read/#limit-the-number-of-documents-to-return

我还强烈建议您阅读教程:http ://docs.mongodb.org/manual/tutorial/getting-started/

于 2013-06-24T08:05:54.640 回答
1

这是我到目前为止所取得的成就

function getFirst20Items() {
    let items

    ITEMS_COLLECTION
        .find({})
        .limit(20)
        .sort({id: 1})
        .toArray( (err, allItems) => {
            items = allItems
        })

    return new Promise(resolve => {
        setTimeout(() => {
            resolve(items)
        }, 2000)
    })
}

function getSecond20Items() {
    let items

    ITEMS_COLLECTION
         // gte stands for --> Greater than
        .find({ id: { $gte: 20 } })
        .limit(20)
        .sort({id: 1})
        .toArray( (err, allItems) => {
            items = allItems
        })

    return new Promise(resolve => {
        setTimeout(() => {
            resolve(items)
        }, 2000)
    })
}

app.get('/first/40/products', (req, res) => {
    const all_Items = []

    getFirst20Items()
        .then(items => {
            all_Items.push(...items)
        })
        .catch(err => {
            throw new CustomError('Could not get Items', err);
        });
    getSecond20Items()
        .then(items => {
            all_Items.push(...items)
        })
        .catch(err => {
            throw new CustomError('Could not get Items', err);
        });
    setTimeout(() => {
        res.send(all_Items);    
    }, 2000);
});

你可以在你的逻辑中从这里继续

希望这可以帮助

于 2019-03-25T10:55:50.903 回答
0
db.getCollection('name of your collection').find({}).limit(20)
于 2017-10-26T10:09:23.073 回答