Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在mongoose中填充后如何执行全文搜索?_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 在mongoose中填充后如何执行全文搜索?

Node.js 在mongoose中填充后如何执行全文搜索?,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我正在构建一个类似twitter的克隆,我正在尝试构建搜索系统,以便对所有tweet数据执行常规文本查找。每个tweet都存储对作者的引用,因此我必须先调用populate,然后才能使用.find() 我试过了,但没有用: const searchTerm = req.query.query; Tweet.find() .limit(10) .populate("author") .find( { $text: { $search: `${searchTerm}` } }) .

我正在构建一个类似twitter的克隆,我正在尝试构建搜索系统,以便对所有tweet数据执行常规文本查找。每个tweet都存储对作者的引用,因此我必须先调用populate,然后才能使用.find()

我试过了,但没有用:

const searchTerm = req.query.query;

Tweet.find()
  .limit(10)
  .populate("author")
  .find( { $text: { $search: `${searchTerm}` } })
  .then(data => {
    res.json({ tweets: data });
  })
  .catch((err) => console.log(err));
这是
Tweet
模型

const TweetSchema = new Schema({
  content: {
    type: String,
    required: true,
  },
  category: {
    type: String,
    required: false,
  },
  comments: [Comment.schema],
  author: { type: Schema.Types.ObjectId, ref: "user", required: true },
  replies: {
    type: Number,
    default: 0,
  },
  likes: {
    type: Number,
    default: 0,
  },
  date: {
    type: Date,
    default: Date.now,
  },
});

TweetSchema.index({ "$**": "text" });

module.exports = Tweet = mongoose.model("tweet", TweetSchema);