Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use aggregate match with UUID type? #12893

Closed
1 task done
jrjake opened this issue Jan 9, 2023 · 6 comments
Closed
1 task done

How to use aggregate match with UUID type? #12893

jrjake opened this issue Jan 9, 2023 · 6 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@jrjake
Copy link

jrjake commented Jan 9, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.8.3

Node.js version

16.17.1

MongoDB version

6.0.3

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Fedora 37

Issue

I understand that mongoose does not automatically cast values in aggregate pipelines, and I've been trying to find how to do this for UUIDs but have been unsuccessful.

My reduced schema:

import mongoose from "mongoose";

const mySchema = new mongoose.Schema({
    _id: {type: mongoose.Schema.Types.UUID},
    name: {type: String, required: true}
});

export const My = mongoose.model("My", mySchema);

What I'm (failing) to do:

let my = await My.aggregate().match({_id: new mongoose.Types.UUID("1e984535-98ad-446b-84cc-bd8f59290eb8")}).exec();

The above fails with TypeError: mongoose.Types.UUID is not a constructor, but this seems to be the solution for ObjectIds (obviously with UUID replaced to ObjectId). How can I aggregate match with UUIDs in mongoose?

@jrjake jrjake added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jan 9, 2023
@FaizBShah
Copy link
Contributor

#1026

Maybe this might help

@FaizBShah
Copy link
Contributor

FaizBShah commented Jan 9, 2023

Also, mongoose does not have any UUID type in mongoose.Types as you can see here: https://github.com/Automattic/mongoose/blob/master/lib/types/index.js

But, as you can see in this file there is a constructor called SchemaUUID() in the UUID Schema Type, so try to use that once and see what's the result

@jrjake
Copy link
Author

jrjake commented Jan 9, 2023

I tried with this as suggested:

let my = await My.aggregate().match({_id: new mongoose.Schema.Types.UUID("1e984535-98ad-446b-84cc-bd8f59290eb8")}).exec();

It didn't crash, but it returns 0 results despite me running the equivalent on the server with MongoDB's UUID() function and it returning the document as expected.

Edit: I also tried accessing SchemaUUID() since it seems to be different from mongoose.Schema.Types.UUID(), but I can't access it because it's not exported (plus import uuid from "../../../node_modules/mongoose/lib/schema/uuid.js"; is super hacky)

@guilhermedalleprane
Copy link

It looks like the native mongodb driver doesn't export the UUID class they use internally like they do with ObjectId, so I don't know how this would be resolved on the mongoose side. But, while no definitive solution is provided, you can generate a UUID using the bson package as the mongodb native driver does by default:

import { UUID } from "bson";
...

let my = await My.aggregate().match({_id: new UUID("1e984535-98ad-446b-84cc-bd8f59290eb8")}).exec();

@jrjake
Copy link
Author

jrjake commented Jan 10, 2023

Thanks @guilhermedalleprane, that's solved my issue. I'll leave this up to the maintainers whether to close or not.

@jrjake
Copy link
Author

jrjake commented Mar 1, 2023

Since MongoDB's latest driver (5.x) exports the UUID class now, I made feature request #13103 to add UUIDs as a type in Mongoose. The best way to get the UUID class with Mongoose 7 is now import {BSON} from "mongodb" and use new BSON.UUID("1e984535-98ad-446b-84cc-bd8f59290eb8")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants