首页 > mongodb > 跳过和限制选项在Meteor / MongoDB中返回Null

跳过和限制选项在Meteor / MongoDB中返回Null (Skip and Limit Options Return Null in Meteor/MongoDB)

问题

我有一个名为“general_roasts”的集合,我试图获取一个随机文档并将其返回。这是db.general_roasts.find()的输出:

meteor:PRIMARY> db.general_roasts.find()
{ "_id" : ObjectId("594b389caad4dc3ae16c5f09"), "text" : "roast 11", "contributor" : "" }
{ "_id" : ObjectId("594b38a1aad4dc3ae16c5f0a"), "text" : "roast 12", "contributor" : "" }
{ "_id" : ObjectId("594b38a5aad4dc3ae16c5f0b"), "text" : "roast 13", "contributor" : "" }
{ "_id" : ObjectId("594b38a7aad4dc3ae16c5f0c"), "text" : "roast 14", "contributor" : "" }
{ "_id" : ObjectId("594b38aaaad4dc3ae16c5f0d"), "text" : "roast 15", "contributor" : "" }

这是代码:

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';

const Categories = new Mongo.Collection('categories');
const GeneralRoasts = new Mongo.Collection('general_roasts');
console.log("GENERAL ROASTS: " + GeneralRoasts.find().fetch());

Meteor.methods({
  'Roasts.random': ({category}) => {
    console.log("received random roast request: " + category);
    if (category == 'general')
    {
      let count = GeneralRoasts.find().count();
      let index = Math.floor(Math.random() * count);
      console.log("count: " + count + " index: " + index);
      //var roast = GeneralRoasts.find({skip: index}).fetch();
      var roast = GeneralRoasts.find({}, {skip: index, limit: 1});
      console.log("returning roast: " + roast.text);
      return roast;
    }
  }
});

Meteor.publish('general_roasts', ()=> {
  console.log("published");
  return GeneralRoasts.find();
});
Meteor.publish('categories', () => {
  return Categories.find();
});

export default GeneralRoasts;

“Roasts.random”的记录输出是:

received random roast request: general
I20170621-22:02:32.059(-7)? count: 5 index: 4
I20170621-22:02:32.060(-7)? returning roast: undefined

有没有人知道为什么应该返回“烤14”时返回null?

提前致谢!

解决方法

正如Neil Lunn所说,你应该接受'烤15'而不是'烤14'。

{skip:0,limit:1}是'roast 11',所以{skip:4,limit:1}将是'roast 15'

你得到了未定义,因为你没有获取它。

更改

var roast = GeneralRoasts.find({}, {skip: index, limit: 1});

var roast = GeneralRoasts.find({}, {skip: index, limit: 1}).fetch();

问题

I have a collection called "general_roasts" and am trying to take a random document and return it. Here is the output of db.general_roasts.find() :

meteor:PRIMARY> db.general_roasts.find()
{ "_id" : ObjectId("594b389caad4dc3ae16c5f09"), "text" : "roast 11", "contributor" : "" }
{ "_id" : ObjectId("594b38a1aad4dc3ae16c5f0a"), "text" : "roast 12", "contributor" : "" }
{ "_id" : ObjectId("594b38a5aad4dc3ae16c5f0b"), "text" : "roast 13", "contributor" : "" }
{ "_id" : ObjectId("594b38a7aad4dc3ae16c5f0c"), "text" : "roast 14", "contributor" : "" }
{ "_id" : ObjectId("594b38aaaad4dc3ae16c5f0d"), "text" : "roast 15", "contributor" : "" }

Here is the code:

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';

const Categories = new Mongo.Collection('categories');
const GeneralRoasts = new Mongo.Collection('general_roasts');
console.log("GENERAL ROASTS: " + GeneralRoasts.find().fetch());

Meteor.methods({
  'Roasts.random': ({category}) => {
    console.log("received random roast request: " + category);
    if (category == 'general')
    {
      let count = GeneralRoasts.find().count();
      let index = Math.floor(Math.random() * count);
      console.log("count: " + count + " index: " + index);
      //var roast = GeneralRoasts.find({skip: index}).fetch();
      var roast = GeneralRoasts.find({}, {skip: index, limit: 1});
      console.log("returning roast: " + roast.text);
      return roast;
    }
  }
});

Meteor.publish('general_roasts', ()=> {
  console.log("published");
  return GeneralRoasts.find();
});
Meteor.publish('categories', () => {
  return Categories.find();
});

export default GeneralRoasts;

The logged output of "Roasts.random" is:

received random roast request: general
I20170621-22:02:32.059(-7)? count: 5 index: 4
I20170621-22:02:32.060(-7)? returning roast: undefined

Does anyone know why null is returned when "roast 14" should be returned?

Thanks in advance!

解决方法

As said by Neil Lunn, you should receive 'roast 15' and not 'roast 14'.

{skip: 0, limit: 1} is 'roast 11', so {skip: 4, limit: 1} will be 'roast 15'

And you get undefined because you didn't fetch it.

Change

var roast = GeneralRoasts.find({}, {skip: index, limit: 1});

to

var roast = GeneralRoasts.find({}, {skip: index, limit: 1}).fetch();
相似信息