简体   繁体   中英

how to find documents that their specific field is not in array type?

Consider documents below:

{'name': 'A', 'link':'All'},
{'name': 'B', 'link':['All','1','2']},
{'name': 'C', 'link':['All']}

I want to find all documents which have field 'link' with type of "string" and value of 'All'. I mean I'm not looking for links in array type. so I want to see the result below:

{'name': 'A', 'link':'All'}

I tried query below but it didn't work and it returns all document which have 'All' either type is string or array.

{link:'All',link:{$type:"string"}}

Actually $type operator looks inside the array field not the field itself.

This may fits you,

Avoiding array objects by checking first element of each link value.

db.getCollection('yourCollection').find({'link.0': {$exists: false}, link: {$type: 2}})

type "2" - means string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM