Meteor Mongo排序未按顺序返回数据

Meteor Mongo排序未按顺序返回数据,meteor,Meteor,我试图从数据库中按从最喜欢到最不喜欢的顺序获取文档,但我一直遇到错误。我创建了一些类似于1、2和3的文档,返回的顺序是2、3、1。这真的很奇怪,因为当我第一次启动服务器时,它工作得很好,但我发现在我的项目上工作了大约20分钟后(没有接触我要发布的代码),我意识到它没有按正确的顺序返回文档。这可能是流星中的虫子吗?还是我这边有问题?无论如何,这是我试图按顺序获取文档的代码 renderNotesByLike.js import React from "react"; import { Tracke

我试图从数据库中按从最喜欢到最不喜欢的顺序获取文档,但我一直遇到错误。我创建了一些类似于1、2和3的文档,返回的顺序是2、3、1。这真的很奇怪,因为当我第一次启动服务器时,它工作得很好,但我发现在我的项目上工作了大约20分钟后(没有接触我要发布的代码),我意识到它没有按正确的顺序返回文档。这可能是流星中的虫子吗?还是我这边有问题?无论如何,这是我试图按顺序获取文档的代码

renderNotesByLike.js

import React from "react";
import { Tracker } from "meteor/tracker";
import { Link, withRouter } from "react-router-dom"

import { Notes } from "./../../methods/methods";

 class RenderNotesByLike extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      notes: []
    };
  }
  renderNotes(notes){
    return notes.map((note) => {
      return(
        <div key={note._id} className="note-list" onClick={() => {this.props.history.push(`/fullSize/${note._id}`)}}>
          <div className="left inline">
            <p><strong>{note.title}</strong></p>
            <span className="removeOnSmallDevice">{note.userEmail}</span>
          </div>
          <div className="right inline">
            <span>Subject: <strong>{note.subject}, {note.unit}</strong></span>
            <br />
            <span className="removeOnSmallDevice">⬆ {note.likes.length} ⬇ {note.dislikes.length}</span>
          </div>
        </div>
      )
    })
  }
  componentDidMount() {
    this.tracker = Tracker.autorun(() => {
      Meteor.subscribe('notes');
      const notes = Notes.find({subject: this.props.subject}, {sort: {likes: -1}}).fetch();
      notes.map((note) => {console.log(note.likes.length)})
      this.setState({ notes })
    });
  }
  componentWillReceiveProps(nextProps) {
    this.tracker = Tracker.autorun(() => {
      Meteor.subscribe('notes');
      const notes = Notes.find({subject: nextProps.subject}, {sort: {likes: -1}}).fetch();
      this.setState({ notes });
    });
  }
  componentWillUnmount() {
    this.tracker.stop()
  }
  render(){
    return(
      <div className="center">
        {this.renderNotes(this.state.notes)}
      </div>
    )
  }
}
export default withRouter(RenderNotesByLike);
我确实意识到一个可能的问题是因为我发布了所有的注释,而我必须发布我想要过滤的注释。但是我用与
CreatedAt
属性完全相同的方法来实现它,而且效果很好

示例数据

cloudinaryData:
{data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
createdAt:
1506224240000
description:""
dislikes:[]
imageURL:["AImageURL.jpg"]
likes:["d@d"]
subject:"Food"
title:"a"
unit:"a"
userEmail:"d@d"
userId:"rSGkexdzzPnckiGbd"
_id:"GPJa8qTZyDHPkpuYo"
__proto__:Object
注释模式:

"notes.insert"(noteInfo){
    noteInfo.imageURL.map((url) => {
      const URLSchema = new SimpleSchema({
        imageURL:{
            type:String,
            label:"Your image URL",
            regEx: SimpleSchema.RegEx.Url
        }
      }).validate({ imageURL:url })
    })

    Notes.insert({
      title: noteInfo.title,
      subject: noteInfo.subject,
      description: noteInfo.description,
      imageURL: noteInfo.imageURL,
      userId: noteInfo.userId,
      userEmail: noteInfo.userEmail,
      unit: noteInfo.unit,
      likes: [],
      dislikes: [],
      createdAt: noteInfo.createdAt,
      cloudinaryData: noteInfo.cloudinaryData
    })
    console.log("Note Inserted", noteInfo)
  } 

您是根据数组而不是数组的长度进行排序的<代码>{sort:{likes:-1}}不会给您提供可预测的结果。尝试使用underline.js函数显式排序获取的文档数组

componentDidMount() {
  this.tracker = Tracker.autorun(() => {
    Meteor.subscribe('notes');
    let notes = Notes.find({subject: this.props.subject}).fetch();
    notes = _.sortBy(notes,(n) => { return n.likes.length});
    this.setState({ notes })
  });
}

您是根据数组而不是数组的长度进行排序的<代码>{sort:{likes:-1}}不会给您提供可预测的结果。尝试使用underline.js函数显式排序获取的文档数组

componentDidMount() {
  this.tracker = Tracker.autorun(() => {
    Meteor.subscribe('notes');
    let notes = Notes.find({subject: this.props.subject}).fetch();
    notes = _.sortBy(notes,(n) => { return n.likes.length});
    this.setState({ notes })
  });
}

你能展示你的
Notes
schema和数据示例吗?@Styx上面更新的你能展示你的
Notes
schema和数据示例吗?@Styx上面更新的我对sortBy()不太熟悉,现在它可以工作了,但它的顺序相反(至少到了greates),我必须如何将其更改为最大到最小?
return-n.likes.length
我对sortBy()不是很熟悉,现在它可以工作,但它的顺序是相反的(从最小到最大),我必须如何将其更改为最大到最小?
return-n.likes.length