2016-09-06 3 views
1

으로 임베디드 된 문서 필드를 기반으로 복합 색인을 작성할 수있는 방법은 무엇입니까? 제목으로 말하면, 서브 문 필드를 기반으로하는 mongoose에 복합 색인을 작성하려고합니다. 이것이 내 스키마의 모양입니다.Mongoose

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

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

나는

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)

아무도 도와 줄 수 있습니까? 사전에 감사드립니다.

답변

0

JSON 속성 구문이 잘못되었으므로 문자열을 정의해야합니다. 키가 따옴표로 정의되어 있지 않으면 .은 키 이름으로 허용되지 않습니다.

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

감사합니다. – WillCed