如何使用Mongoose基于嵌入式文档字段构建复合索引

时间:2016-09-06 20:41:40

标签: mongoose

正如标题所说,我试图根据子文档字段在mongoose上创建一个复合索引,这就是我的架构的样子。



pricingSchema = new Schema({
	retail:{ //normal retail price
		type:Number
	},
	retailOnDiscount:{ //retail during solde
		type:Number
	},
	savings:{ //price which will be saved by the customer
		type:Number
	},
	pct_savings:{ //saving percentage
		type:Number
	}
});

ar ProductSchema = new Schema({                                                                                                                                                                                    
	   sku: {                                                                                                                                                                                                            
	    type: Number                                                                                                                                                                                                    
	  },                                                                                                                                                                                              
	  kind: {                                                                                                                                                                                                           
	    type: String                                                                                                                                                                                                    
	  },                                                                                                                                                                                                                
	  title: {                                                                                                                                                                                                          
	    type: String                                                                                                                                                                                                    
	  },	                                                                                                                                                                                                                                                                                                                                                                                                                            
	  shipping:shippingSchema,
	  pricing:pricingSchema,                                                                                                                                                                                                                                                                                                                                                                                                             
	  details:detailSchema,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
	  description: {                                                                                                                                                                                                    
	    type: String                                                                                                                                                                                                    
	  },                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
	  imageUrl: {                                                                                                                                                                                                         
	    type: Array                                                                                                                                                                                                        
	  }                                                                                                                                                                                                                                                                                                                                                                                                                      
}); 




我已尝试过以下命令



ProductSchema.index({pricing.pct_savings:1,type:-1});




但是我收到了这个错误:



/Users/willy/workspace/dev/le-beau-cheveu/dev/backend-v2/server/api/product/productModel.js:111
ProductSchema.index({pricing.pct_savings:1,type:-1});//compound index at the schema level
                            ^

SyntaxError: Unexpected token .
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/Users/willy/workspace/dev/le-beau-cheveu/dev/backend-v2/server/api/product/productController.js:1:77)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/Users/willy/workspace/dev/le-beau-cheveu/dev/backend-v2/server/api/product/productRoutes.js:3:18)
    at Module._compile (module.js:413:34)
&#13;
&#13;
&#13;

任何人都可以帮助我,提前谢谢你。

1 个答案:

答案 0 :(得分:0)

它只是错误的JSON属性语法,您需要将键定义为字符串。密钥名称中不接受.,除非使用引号

定义密钥
ProductSchema.index({'pricing.pct_savings':1,type:-1});