euclideanDistance
On this page
Returns the scalar Euclidean distance between two vector inputs.
Syntax
{ $euclideanDistance: [ <expression>, <expression> [, <size>]] }
Arguments
-
expression1
,expression2
: An expression that resolves to a binary value. -
size
(Optional): A string that specifies the data type of the vector elements.The default is "F32"
.The following values are supported: String
Description
I8
8-bit signed integer
I16
16-bit signed integer
I32
32-bit signed integer
I64
64-bit signed integer
F32
32-bit floating-point number (IEEE standard format)
F64
64-bit floating-point number (IEEE standard format)
Example
The following MongoDB® Shell script compares face embeddings using euclideanDistance
:
col = db.faces;db.createCollection("faces", {columns: [{ id: "faceEmbedding", type: "LONGBLOB NULL" }],});col.insertMany([{_id: ObjectId("642cf6870d149e904828642d"),name: "Face A",faceEmbedding: BinData(0,"kwipPa80Br1FKPA8D5mbvI9ZeDzZLey8OA5VPbv/FD1GIAU9sq8fvXB5lTxAg5o7QILtO+2tsDxucUO9akF0PBIFHT0tcdK8R/FovWs0bDwtHZu7rFNVvaAJQj3F/bY8Z1CkvJbqEj2GvBW9PitMO6oJLj3OEio8Fe5Ouwm1Ab0+1WG9vNu0vQy5M7x0g128o5cSvd9HB71CEuO87LkTPK0b+TvYXoQ9v3RDvQDdlT39YlI9d2q4u7DKpzwdQq49FxYuOcik/bulySq8ZpWFu7NAZbwfrVm8tl7evE44xz2Rs5G99AacvFOy47ww4AY7UJPAuNqU5rubd0C9C8WYPLP2mzvTrXm88PpHvaRLUz1mfxc94+gXvQdrrjtV39I9S+cHvbGp4rwt93U7L2ZovZqBQD1l7nO7Saupun5F9TwRFCm9DEJMuxupX72ZkYE9xkcOPQJf2bvk1Fw9ZeN9PFfNWz02bPA8wn+1PJUlnr2iFV89XPLnO17cu7wAmFG9rSZZvH46jjy98ss8KIwYvADC5Du1Iag8Ob8CvRZlcT2dzbA9oMkGPcQ7vLw/35I9Aw2KPbkrgbzsEtQ9YA4APUbBsjzRBDk8M6J9PN6BJr2bmE08ed2dvYtk37xRiEK9BN++Pd9zIT13nA29xlGXvLhn2zu5pEC8AeMo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vEi7tTxR0ys9LifNPan56T2/D569z+gwPafHgz2I9gM87MhvPB/5Mz36rLY8UHOwOzuTFj17sw+9NlSzPFW0gT2F6re8jDCWPKdFhr2c0Mo9hxK7vDcFrz21PYK9FEMCvI1ymzzklNG7Lz+Kvd0d6DxvaT68e7stvRa1xjzVwiM9gvMEPcQ6Cr338HY8EsIjPXAGtb2kOXU9mvHtvCHLL70yGji9Ob1QPY8KTbtN5eI8JRRYPYAWP7wcIEg9ZWAIPdPJ1jxo35K8a3wWvH731r2TawM+gWLNPCzDoT0L0zQ92L82PUPOmrypRny9ar5YPIDkpLxiX6q9zYnZPHerAr1sR487Y+WYPaIxP71eoka7wsiXvQvnu7q48jA9RJufPFVG9Dnitj69cLY4OzJOir3Fooe9apohPHJyuLyCncM9/4d5PaxQqb3k8J29g37+PG+Hc7yFiys8iGZQPVFteT2wMPK7G1gxPX9w0zzmDEW9s1pFvQc56TzQ5DS9Pox/vFvxe70k++q8DjiZvPoenzkSiwY9loebvO0CZz1JYti96rXau1JiGj2m//M9ClF2PNCmpj1jVDE889wXPfSP37yWByS9QeoOuyePC73Phxm72tYbvV4Uhz1h6/y7k0MBPCl8Pb3QRgE9yhQIPRgOWT3RL+O8lo4UvH7SxLy2kxG9532WvVnzOj0xzTK9HToRPPtKIbw+EwI8RXu1vBY2AT1h04K7PqKVO1UsrbyWYAU7MUxPPQEqZzxJNJy9I9FbvSzxrbzphM48AQDKutkdJb1bLEu9PPAVvWm54D1yTni8rvoIPeDUpbyF1Mk82gCFumiPhjzSLl09pYWlvdzYHz0nMs47oTMqvbXHjLxV1MI8UsytPOjLz72Cgum8jI+RvDxbY7xYOlm9M4UBvNvlWb0v5ae94tsFPWC6JjxcptU8qNQwPVtyI7oeV3e9NF3ePMf+iL0="),},{_id: ObjectId("642cfad80d149e9048287578"),name: "Face B",faceEmbedding: BinData(0,"3Rm8PcFNM70Zprg9fynmO/0pi7uUAf68btVnPBYBnbzHo5881P9Vvd5w9DxIT1i9yrgXPboQy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YOj1CeZA9/kmRPYlGibuq03Q8RLzAPNMwA74="),},]);var testFace = col.findOne({ name: "Face A" });col.aggregate([{$addFields: {score: {$euclideanDistance: ["$faceEmbedding", testFace.faceEmbedding, "F32"],},},},{ $sort: { score: 1 } },{ $project: { name: 1, score: 1 } },]);
[
{
_id: ObjectId("642cf6870d149e904828642d"),
name: 'Face A',
score: 0
},
{
_id: ObjectId("642cfad80d149e9048287578"),
name: 'Face B',
score: 0.7802467771327307
},
{
_id: ObjectId("642cfa330d149e90482872ac"),
name: 'Face C',
score: 0.8352553534520302
}
]
Last modified: November 10, 2023