MongoDB => findOne or find the query of the next object? (Meteor / Reaction)

Want to find the next and previous object in relation to the current.

This is what I have

this.props._id = currentId;

// Fetch current object data
data.video = Videos.findOne({_id: this.props._id});

// Using votes string from object above to find me objects 
data.next = Videos.findOne({votes: {$gte: data.video.votes}});
data.previous = Videos.findOne({votes: {$lte: data.video.votes}};

I know that this is not true, I’m sure that it will return objects, but it will not be the nearest object, and there is a chance that I will return the current object.

What I want to do is return the next or previous object, where my selector is voices, I also want to use Id to exclude the current object, then there is also a good chance that several objects will have the same number of votes.

It was 12 hours right now, and I’m almost going back to where I started, so I would really appreciate some examples to make me wrap my head around this, Not sure if I should use find or findOne.

VideoPage = React.createClass({
  mixins: [ReactMeteorData],
  getMeteorData() {
    var selector = {};
    var handle = Meteor.subscribe('videos', selector);
    var data = {};
    data.userId = Meteor.userId();
    data.video = Videos.findOne({_id: this.props._id});
    data.next = Videos.findOne({votes: {$gte: data.video.votes}});
    data.previous = Videos.findOne({votes: {$lte: data.video.votes}};
    console.log(data.video.votes);
    console.log(data.video);
    console.log(data.next);
    console.log(data.previous);


    return data;
  },


  getContent() {
    return <div>
    {this.data.video.votes}
      <Youtube video={this.data.video} />
    <LikeBox next={this.data.next._id} previous={this.data.previous._id} userId={this.data.userId} video={this.data.video} />
    </div>
    ;
  },


  render() {
    return <div>

      {(this.data.video)? this.getContent() :
        <Loading/>
      }

    </div>;
  }
});
+4
1

:

  • id

JS:

data.video = Videos.findOne({ _id: currentId });

// object with next highest vote total
data.next = Videos.findOne({ _id: { $ne: currentId },
  votes: { $gte: data.video.votes }},{ sort: { votes: 1 }});

// object with next lowest vote total
data.previous = Videos.findOne({ _id: { $ne: currentId },
  votes: { $lte: data.video.votes },{ sort: { votes: -1 }});
+1

Source: https://habr.com/ru/post/1614966/


All Articles