2016-09-06 5 views
1

で埋め込まれたドキュメントフィールドに基づいて複合インデックスを構築するにはどうすればいいですか?タイトルによれば、私はサブドキュメントフィールドに基づいて複合インデックスを作成しようとしています。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