Keystone JS / JS Mongo查询,如何使用URLSearchParams作为键来查询数据

时间:2019-09-04 03:19:00

标签: javascript node.js mongodb mongodb-query keystonejs

想进行动态查询,在该查询中将根据匹配结果的urlSearchParams数据返回数据。它应该从数据库返回数据,其中键和值与urlSearchParams匹配,例如与所有Year(2019年和2018年)等等。

这是我在制作时遇到麻烦的部分

基于urlSearchParams的动态数据,如何使其能够查询并返回urlSearchParams中匹配的数据({'Year'=>'2019,2018','Make'=>'Acura,BMW' })

q.where(urlSearchParams key should be here).in([urlSearchParams]);

URL搜索参数

{ 'Year' => '2019,2018', 'Make' => 'Acura,BMW' }

查询代码

view.on('init', function (next) {

        var q = keystone.list('Post').paginate({
            page: req.query.page || 1,
            perPage: 2,
            maxPages: 2,
            filters: {
                state: 'published', 
            },
        })
            .sort('-publishedDate')
            .populate('author categories');

        if (locals.data.category) {

            //Where key is in urlSearchParams for example based on this searchparams
            { 'Year' => '2019,2018', 'Make' => 'Acura,BMW' }
            It should return the data from the database where Key and values that matches from urlSearchParams
            like all the Year which is 2019 and 2018 so on and so forth
            q.where('Key Here ').in([urlSearchParams]);
        }

        q.exec(function (err, results) {
            locals.data.posts = results;

            next(err);
        });
    });

0 个答案:

没有答案