【发布时间】:2014-02-09 00:49:42
【问题描述】:
来自 mongodb 文档:
基本上为子文档创建索引包括编写类似"address.city"
到目前为止,我还不能这样做。
我试过了:
index = "category.code";
index = { "category.code": 1 };
index = { "category": {code: 1} };
collection.ensureIndex( index, {unique: true, w: 1}, function(error, data){
console.log(error ? error : documentName + ': [ensureIndex] ' + JSON.stringify(data));
});
但没有创建索引。
还有其他解决办法吗?
当我通过mongo 从命令行执行此操作时,它可以工作。
db.type.ensureIndex({'category.code':1})
但是当我从 JS 脚本运行它时,它不会。
var mongodb = require('mongodb');
exports.up = function(db, next){
var documentName = 'type';
var collection = mongodb.Collection(db, documentName);
...
【问题讨论】: