0

I have collection of documents like this:

{
    _id: ObjectId('5e7638edfe406adbff385784'),
    chat: 109081533,
    user: 109081533,
    data: {
        name: 'Max',
        address: {
            '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7': {
                transfer_orders: {
                    '1584806368': {
                        state: 'new',
                        from: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
                        to: '0x590631dd6f3ef395ca46ad5108f4fb4ab9ef0cd4',
                        amount: '22',
                        data: 'some info'
                    },
                    '1584806512': {
                        state: 'pending',
                        from: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
                        to: '0xFdd8b50f36a3810b7971dd9631f885E2f2Fb400C',
                        amount: '33',
                        data: 'some info'
                    },
                    '1584898932': {
                        state: 'new',
                        from: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
                        to: '0x590631dd6f3ef395ca46ad5108f4fb4ab9ef0cd4',
                        amount: '121',
                        data: 'some info'
                    },
                    '1584924924': {
                        state: 'ok',
                        from: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
                        to: '0xb2aff88b085b4f01ea89b39407c04faa962e153d',
                        amount: '222',
                        data: 'some info'
                    },
                    '1585144560': {
                        state: 'new',
                        from: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
                        to: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
                        amount: '22',
                        data: 'some info'
                    }
                },
                income: {}
            }
        }
    }
}
  1. How to project inner objects where state =='new' ?
  2. How to find documents where inner objects where state =='new' ?

I have tried dot notation like this find_one({"data.address.0.transfer_orders.0": {state: "new"}}). But address and transfer_orders elements are not arrays. I have found that find({"data.address": {$type: 3}}) is work for my collection. Hence address element is object type. But how to query it?

Max S
  • 3
  • 2

1 Answers1

0

For objects with id keys in not an easy task to write a query.

I assume your keys (0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7, 1584898932) aren't part of the db schema, therefore they cannot be indexed

However, for array you can use operators like $elemMatch see here: Query an Array

I suggest you to convert your maps (objects) to arrays instead

transfer_orders: [
  'key': '1584806368',
  'value': {
      state: 'new',
      from: '0x3cd68b454bce1c7be95a3587bfd71d1be9bb85a7',
      to: '0x590631dd6f3ef395ca46ad5108f4fb4ab9ef0cd4',
      amount: '22',
      data: 'some info'
   }
]
ijavid
  • 715
  • 12
  • 23