使用Loopback从MongoDB获取随机对象

时间:2021-07-15 16:52:04

I have searched all over the web for a good answer to how I can get multiple random objects from MongoDB using Loopback.

我已经在网上搜索了一个很好的答案,我可以使用Loopback从MongoDB中获取多个随机对象。

The reason why I search for a good way to show random data is for a module like "Interesting in this book/recommendation", that shows 4-5 books randomly.

我搜索一个显示随机数据的好方法的原因是为了一个模块,如“有兴趣阅读本书/推荐”,随机显示4-5本书。

Hopefully, you guys can bring me in a good direction how to solve the issue the best way.

希望你们能带给我一个好方向,如何以最好的方式解决问题。

1 个解决方案

#1


0  

To get a random object from MongoDB using Loopback, you will need to create a custom endpoint (in your model.js file), like so:

要使用Loopback从MongoDB获取随机对象,您需要创建一个自定义端点(在您的model.js文件中),如下所示:

Model.random = function(cb){
    // this next line is what connects you to MongoDB
    Model.getDataSource().connector.connect(function(err, db) {
      var collection = db.collection('YOURCOLLECTIONNAMEHERE');
      //the below is MongoDB syntax, it basically pulls a 
      //random sample of size 1 from your collection
      collection.aggregate([
        { $sample: { size: 1 } }
      ], function(err, data) {
        if (err) return cb(err);
        return cb(null, data);
      });
    });
}

Replace "Model" with your model name. Note: it needs to be capitalized! If you don't know your collection name, if Loopback created it, it is usually the same as your model name (not capitalized).

将“型号”替换为您的型号名称。注意:它需要大写!如果您不知道您的集合名称,如果Loopback创建它,它通常与您的模型名称相同(不是大写)。

To make this endpoint show up in the Explorer, add this to your model.js file too:

要使此端点显示在资源管理器中,请将其添加到model.js文件中:

Model.remoteMethod(
    'random', {
        http: {
            path: '/random',
            verb: 'get'
        },
        description: 'Gets one random thing',
        returns: {
            arg: 'randomThings',
            type: 'json'
        }
    });

I have an example of this endpoint in this sample application: jeopardy-mongo-api

我在此示例应用程序中有一个此端点的示例:jeopardy-mongo-api

#1


0  

To get a random object from MongoDB using Loopback, you will need to create a custom endpoint (in your model.js file), like so:

要使用Loopback从MongoDB获取随机对象,您需要创建一个自定义端点(在您的model.js文件中),如下所示:

Model.random = function(cb){
    // this next line is what connects you to MongoDB
    Model.getDataSource().connector.connect(function(err, db) {
      var collection = db.collection('YOURCOLLECTIONNAMEHERE');
      //the below is MongoDB syntax, it basically pulls a 
      //random sample of size 1 from your collection
      collection.aggregate([
        { $sample: { size: 1 } }
      ], function(err, data) {
        if (err) return cb(err);
        return cb(null, data);
      });
    });
}

Replace "Model" with your model name. Note: it needs to be capitalized! If you don't know your collection name, if Loopback created it, it is usually the same as your model name (not capitalized).

将“型号”替换为您的型号名称。注意:它需要大写!如果您不知道您的集合名称,如果Loopback创建它,它通常与您的模型名称相同(不是大写)。

To make this endpoint show up in the Explorer, add this to your model.js file too:

要使此端点显示在资源管理器中,请将其添加到model.js文件中:

Model.remoteMethod(
    'random', {
        http: {
            path: '/random',
            verb: 'get'
        },
        description: 'Gets one random thing',
        returns: {
            arg: 'randomThings',
            type: 'json'
        }
    });

I have an example of this endpoint in this sample application: jeopardy-mongo-api

我在此示例应用程序中有一个此端点的示例:jeopardy-mongo-api