wheresvic / mongoose-field-encryption

A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.
MIT License
74 stars 32 forks source link

Cannot use Mongoose Operators when using find or findOne #108

Closed rubencosta13 closed 1 year ago

rubencosta13 commented 1 year ago

Greetings!

I'm using mongoose-field-encryption to encrypt specific fields on a model... However, I need to find data with a mongoose operator: $in: Which refuses to work with mongoose-field-encryption!

Here are the code snippets I'm using:

Create the users:

const createUser = new User({
    name: "Maria", 
    attributes: [
      "Self Taught SWE",
      "Developer"
    ]
  })
const createBogusUser = new User({
    name: "John", 
    attributes: [
      "Self Taught SWE",
    ]
  })
await createUser.save();
await createBogusUser.save()

My goal is to find all the users that have the attribute "Developer", which, without mongoose-field-encryption can be done as follows:

Source: Docs

await User.find({ attributes: { $in: ["Developer"] });

However if I try to run the same query, on a model that has mongoose-field-encryption, it will return all the documents, even though only the user "Maria" has the developer attribute...

Is there any way to fix this?

rubencosta13 commented 1 year ago

@wheresvic Any updates here?

Also my populates arent working...

here's a sample of my model:

const userModel = new mongoose.Schema({
  user: {
    type: String,
    required: true
  },
  userAttributes: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'UserAttributes',
    },
  ],
});

const userAttributes = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  attributes: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'UserAttributes',
    },
  ],
  status: {
    type: Boolean,
    required: true
  }
});

const attributes = new Attributes({
  name: 'Developer',
  attributes: [
    'Developer',
    'SWE',
    'some random entry'
  ],
  status: true
});

const user = new User({
  user: "John",
  attributes: attributes._id
});

attributes.save();
user.save();

if I try to populate the attributes on the user, it just returns me null, on the whole query

ThamerAlluqmani commented 11 months ago

Hi @rubencosta13 , I'm facing same issue. Did you solve it ?