[英]Mongo text index on populated fields
Im just learning indexing with Mongoose/MongoDB and I dont know why this isnt working.我只是在学习使用 Mongoose/MongoDB 进行索引,我不知道为什么这不起作用。
this is my schema这是我的架构
const timeSchema = new mongoose.Schema({
actionId:{
type:String,
required:true
},
start: {
type: Date
},
end: {
type: Date
},
user:{type : mongoose.Schema.Types.ObjectId, ref : 'User'},
task:{type : mongoose.Schema.Types.ObjectId, ref : 'Task'},
pausedSeconds:{
type: Number,
default: 0
}
});
const Time = mongoose.model('Time', timeSchema)
i want to have a text index in two populated fields user and task, i created the index this way我想在两个填充字段用户和任务中有一个文本索引,我以这种方式创建了索引
timeSchema.index({"user.name":"text","task.taskName":"text"})
Here is an example of the documents这是文档的示例
{
"pausedSeconds": 18,
"_id": "5db1dde8d5bc93526c26fa38",
"actionId": "5feaebcf-6b90-45be-8104-452d643472a0",
"user": {
"_id": "5d4af77e4b6cbf3dd8c5f3ac",
"name": "admin"
},
"task": {
"_id": "5d4aff2f61ad755154b8a1c6",
"taskName": "task 1 updated!"
},
"start": "2019-10-24T17:22:48.000Z",
"end": "2019-10-24T17:30:00.000Z"
},
I have one issue and one question The issue is:我有一个问题和一个问题问题是:
What im trying to do is get all the documents that have "task 1 updated" (for task.taskName) or "admin" (for user.name) doing it this way我想要做的是让所有具有“更新任务 1”(对于 task.taskName)或“admin”(对于 user.name)的文档都这样做
Time.find({ '$text': { '$search': "admin" } })
Time.find({ '$text': { '$search': "task 1 updated" } })
but it doesnt seem to work但它似乎不起作用
The question is: If I want to do a text search for the fields start,end being a Date type or for the field pausedSeconds being a Number type what should I do?问题是:如果我想对字段 start,end 作为 Date 类型或字段 pausedSeconds 是 Number 类型进行文本搜索,我该怎么办?
Thanks in advance提前致谢
In your query, you aren't specifying what property to search on.在您的查询中,您没有指定要搜索的属性。 Do this:
Time.find({taskName: { '$text': { '$search': "admin" }}})
.这样做:
Time.find({taskName: { '$text': { '$search': "admin" }}})
。
Also, I'm not sure if you're just not showing all the code or if you're actually doing your query wrong, but it should be written like this:另外,我不确定您是否只是没有显示所有代码,或者您实际上是否在查询错误,但应该这样写:
Time.find({taskName: { '$text': { '$search': "admin" }}}).exec(function(err, times) {
if(err) return console.log(err);
console.log(times);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.