Optional
includeIf true, include the similarity score in the result via the $similarity
field.
If false, do not include the similarity score in the result.
Defaults to false.
false
const doc = await collection.findOne({}, {
sort: {
$vector: [.12, .52, .32],
},
includeSimilarity: true,
});
console.log(doc?.$similarity);
Optional
includeIf true, fetch the sort vector on the very first API call.
If false, it won't fetch the sort vector until FindCursor.getSortVector is called.
Note that this is not a requirement to use FindCursor.getSortVector—it simply saves it an extra API call to fetch the sort vector.
Set this to true if you're sure you're going to need the sort vector in the very near future.
const doc = await collection.findOne({}, {
sort: {
$vector: [.12, .52, .32],
},
includeSortVector: true,
});
// sortVector is fetched during this call
const next = await cursor.next();
// so no I/O is done here as the cursor already has the sortVector cached
const sortVector = await cursor.getSortVector();
Optional
limitMax number of documents to return. Applies over the whole result set, not per page. I.e. if the
result set has 1000 documents and limit
is 100, only the first 100 documents will be returned,
but it'll still be fetched in pages of some N documents, regardless of if N < or > 100.
Optional
projectionSpecifies which fields should be included/excluded in the returned documents.
If not specified, all fields are included.
When specifying a projection, it's the user's responsibility to handle the return type carefully, as the projection will, of course, affect the shape of the returned documents. It may be a good idea to cast the returned documents into a type that reflects the projection to avoid runtime errors.
interface User {
name: string;
age: number;
}
const collection = db.collection<User>('users');
const doc = await collection.findOne({}, {
projection: {
_id: 0,
name: 1,
},
vector: [.12, .52, .32],
includeSimilarity: true,
}) as { name: string, $similarity: number };
// Ok
console.log(doc.name);
console.log(doc.$similarity);
// Causes type error
console.log(doc._id);
console.log(doc.age);
Optional
skipNumber of documents to skip. Only works if a sort is provided.
Optional
sortThe order in which to apply the update if the filter selects multiple documents.
If multiple documents match the filter, only one will be updated.
Defaults to null
, where the order is not guaranteed.
null
Optional
vectorAn optional vector to use of the appropriate dimensionality to perform an ANN vector search on the collection to find the closest matching document.
This is purely for the user's convenience and intuitiveness—it is equivalent to setting the $vector
field in the
sort field itself. The two are interchangeable, but mutually exclusive.
If the sort field is already set, an error will be thrown. If you really need to use both, you can set the $vector field in the sort object directly.
sort: { $vector: [...] }
insteadOptional
vectorizeAkin to FindOptions.vector, but for $vectorize
.
sort: { $vectorize: '...' }
instead
Options for the
find
method.Field
sort - The sort order to pick which document to return if the filter selects multiple documents.
Field
projection - Specifies which fields should be included/excluded in the returned documents.
Field
limit - Max number of documents to return in the lifetime of the cursor.
Field
skip - Number of documents to skip if using a sort.
Field
includeSimilarity - If true, include the similarity score in the result via the
$similarity
field.See
Collection.find