MongoDB=>findOne还是find查询下一个对象?(Meteor/React)
MongoDB => findOne or find querying next object? ( Meteor / React )
想要查找与当前对象相关的下一个和上一个对象。
这就是我拥有的
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}};
我知道这是不正确的,当然它会返回对象,但它不会是最近的对象,我也有机会返回当前对象。
我想做的是返回我的选择器是投票的下一个或上一个对象,我还想确保使用Id来排除当前对象,然后几个对象也很有可能具有相同的投票数。
我已经连续做了12个小时了,现在已经回到了我开始的地方,所以我真的很感激一些例子让我思考这个问题,不再确定我应该使用find还是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>;
}
});
您需要:
- 排除当前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 }});
相关文章:
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- AngularJS&JSON-从Previous&下一个对象
- jQuery$.inArray()总是返回-1和一个对象数组
- javascript处理一个对象数组以获得一个新的对象数组
- javascript函数,它接受两个输入:一个对象和一个键,并返回对象中该键的相应值
- 你能用来自数组的属性名称生成一个对象吗
- 预期响应包含一个对象,但在angular js中得到一个数组错误
- Protractor:element.getText()返回一个对象,而不是String
- 如何使用jQuery添加另一个对象的高度作为边距
- 计算从一个对象到另一个对象的路径并沿其移动
- 如何将一个对象添加到每个对象数组中
- 为了避免创建全局变量,可以将所有变量分配给一个对象吗
- 将javascript对象(属性+值)合并到一个对象中
- 尝试简化检查对象键是否为true并将其推送到另一个对象
- 从 javascript 中的对象方法返回一个对象
- 为什么 Meteor.users.findOne({用户名: “test”}) 返回一个对象,但用具有相同值的变量替换
- Meteor:Publish函数返回整个集合,而不是一个对象
- MongoDB=>findOne还是find查询下一个对象?(Meteor/React)