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
db.products.createIndex(
{ name: "text", address: "text" },
{ weights: { name: 5, address: 1 } }
);