Articles → MONGODB → Assign Weights To Each Field While Creating An Index In Mongodb

Assign Weights To Each Field While Creating An Index In Mongodb






Purpose





Syntax


db.collection.createIndex(
   { [field1]: "text", [field2]: "text", ... },
   { weights: { [field1]: [weight1], [field2]: [weight2], ... } }
)



Example




Picture showing the sample document collection




db.products.createIndex(
   { name: "text", address: "text" },
   { weights: { name: 5, address: 1 } }
);


Picture showing assigning weights while creating an index in mongodb



Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, May 14, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250