1.0.8 • Published 2 years ago

mongodb-query-helper v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

How to install

npm install mongodb-query-helper

How to use

import { convertNested } from 'mongodb-query-helper'

const nestedQuery = {
    'fullName': {
        'name': 'Yusuf',
        'surname': 'Eren'
    },
    'externalId': 1,
    'address': {
        'city': 'Istanbul'
    }
}

convertNested(nestedQuery);
/*
{
  'fullName.name': 'Yusuf',
  'fullName.surname': 'Eren',
  externalId: 1,
  'address.city': 'Istanbul'
}
*/

Why I built this?

I was struggling to update specific fields with nested objects in MongoDB documents. MongoDB query was replacing the nested object instead of setting the new fields. as an example;

// mongoshell

// Insert document
db.users.insertOne({ userId: 1, fullName: { 'name': 'Yusuf', 'surname': 'Eren'} })
// Output
[
  {
    _id: ObjectId('65c8f50d7b3b4b02d130e396'),
    userId: 1,
    fullName: { name: 'Yusuf', surname: 'Eren' }
  }
]

// Update name inside fullName
db.users.updateOne({ userId: 1 }, { $set: { fullName: { 'name': 'Yusuf' } } })

// Read the updated doc
db.users.findOne({ userId: 1 })
// Output
{
  _id: ObjectId('65c8f50d7b3b4b02d130e396'),
  userId: 1,
  fullName: { name: 'Yusuf' }
}

So I decided to build that package. Now, it converts the nested objects to mongodb's update structure to update single fields instead of replacing all fields of the object.

Contribution & Support

If you want to contribute to package go to repository and create a pull request with a description :) If you need support please contact me via erenyusuf170@gmail.com

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago