當運行我的測試時,我收到一個錯誤,說明我沒有在標題字段上設置文本索引,但運行我的應用程序時文本搜索工作正常模型,不要拋出錯誤。我使用ava用於測試和這裏$文本查詢(沒有這樣的集合「測試db.torrents」)所必需在查詢運行前沒有創建貓鼬索引
文本索引
import mongoose from 'mongoose';
import Category from './category';
const Schema = mongoose.Schema;
const Torrent = new Schema({
title: {
type: String
},
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category',
required: true,
index: true
},
size: Number,
details: [
{
type: String
}
],
swarm: {
seeders: Number,
leechers: Number
},
lastmod: {
type: Date,
default: Date.now()
},
imported: {
type: Date,
default: Date.now()
},
infoHash: {
type: String,
unique: true,
index: true
}
});
Torrent.index({
title: 'text'
}, {
background: false
});
export default mongoose.model('Torrent', Torrent);
是我測試的情況。
import mongoose from 'mongoose';
import request from 'supertest';
import test from 'ava';
import {makeApp} from '../helpers';
test.before(() => {
mongoose.Promise = Promise;
mongoose.connect('mongodb://localhost:27017/astro-test-db');
});
test.after.always(() => {
mongoose.connection.db.dropDatabase(() => {
mongoose.disconnect();
});
});
// Should return [] since HL3 doesn't exist.
test('should return no search results', async t => {
const app = makeApp();
const res = await request(app).get(`/api/search?q=HL3`);
t.is(res.status, 200);
t.is(res.body.error, {});
t.is(res.body.torrents.length, 0);
});
下面是從AVA全輸出中,你可以看到標題索引不會與「文」或background: false
創建。
➜ astro git:(develop) ✗ yarn ava test/routes/search.js -- --verbose
yarn ava v0.24.6
$ "/Users/xo/code/astro/node_modules/.bin/ava" test/routes/search.js --verbose
Mongoose: categories.ensureIndex({ title: 1 }, { unique: true, background: true })
Mongoose: torrents.ensureIndex({ category: 1 }, { background: true })
Mongoose: torrents.count({}, {})
Mongoose: categories.ensureIndex({ slug: 1 }, { unique: true, background: true })
Mongoose: torrents.find({ '$text': { '$search': 'x' } }, { limit: 100, sort: { score: { '$meta': 'textScore' }, 'swarm.seeders': -1 }, fields: { score: { '$meta': 'textScore' } } })
✖ should return no search results
1 test failed [13:59:07]
should return no search results
/Users/xo/code/astro/test/routes/search.js:24
23: t.is(res.status, 200);
24: t.is(res.body.error, {});
25: t.is(res.body.torrents.length, 0);
Difference:
- Object {
- code: 27,
- codeName: "IndexNotFound",
- errmsg: "text index required for $text query (no such collection \'astro-test-db.torrents\')",
- message: "text index required for $text query (no such collection \'astro-test-db.torrents\')",
- name: "MongoError",
- ok: 0,
- }
+ Object {}
_callee$ (test/routes/search.js:24:7)
tryCatch (node_modules/regenerator-runtime/runtime.js:65:40)
Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:303:22)
Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:117:21)
step (test/routes/search.js:19:191)
error Command failed with exit code 1.
你是否將你的集合放入測試中?在創建索引之前可能會有查詢開始執行的情況。在'mongoose.connect()'被調用之前設置'mongoose.set(「debug」,true)''看到了什麼? –
所有測試完成後,系列都會丟失。我完全忘記了貓鼬的調試模式。我會檢查並回來。 –
經過一些測試後,它看起來像mongoose添加了'background:true'來創建索引,這意味着索引是在測試完成後創建的。 –