JavaScript Mongo db query for finding object type

I've 1000's of users and user data looks like the below. Some users have the devices as [{some data}] (array), some users have devices as {some data}, and some users have devices as empty [].

I need mongodb query to find the userslist with devices {some data}. Here is the sample of my users data.

{
    "_id" : ObjectId("56fe07bab95708fa18d45ac4"), 
    "username" : "abcd",    
    "devices" : []
},
{
    "_id" : ObjectId("56fe07bab95708fa18d45df7"), 
    "username" : "efgh",    
    "devices" : [ 
        {
            "_id" : ObjectId("5827804ef659a60400e12fcb"),
            "devicetype" : "web"
        }
    ],
},
{
    "_id" : ObjectId("56fe07bab95708fa18d45ae8"), 
    "username" : "efgh",    
    "devices" : {
         "_id" : ObjectId("5951ea8b47abe300046ea26e"),
         "devicetype" : "web"
     }
},
{
    "_id" : ObjectId("56fe07bab95708fa18d45b5b"), 
    "username" : "ijkl",    
    "devices" : [ 
        {
            "_id" : ObjectId("59bd2317eeff3200049a2ba6"),
            "devicetype" : "ios"
            "devicetoken" : "1abffaa4419d498b48d0bf982"
        }
    ],
},
{
    "_id" : ObjectId("56fe07bab95708fa18d46102"), 
    "username" : "efgh",    
    "devices" : {
         "_id" : ObjectId("58c433da28841d00040d3cdb"),
         "devicetype" : "web"
     }
},
{
    "_id" : ObjectId("56fe07bab95708fa18d46177"), 
    "username" : "efgh",    
    "devices" : {
         "_id" : ObjectId("59d073d96974d20004a4bb9f"),
         "devicetype" : "web"
     }
},
{
    "_id" : ObjectId("56fe07bab95708fa18d456c9"), 
    "username" : "ijkl",    
    "devices" : [ 
        {
            "_id" : ObjectId("59b93dd2e6673c00044cca49"),
            "devicetype" : "ios"
            "devicetoken" : "1abffaa4419d498b48d0bf982"
        }
    ],
},
{
    "_id" : ObjectId("56fe07bab95708fa18d456f4"), 
    "username" : "abcd",    
    "devices" : []
}
Answer:1

You can use $type operator like this

db.collection.find( { "devices" : { $type : "object" } } );

or

db.collection.find({ "devices": { $not: { $type: "array" } }})

Answer:2

I want to make a function that takes an array as an argument, creates a random number from 1 - 10, and runs until it creates a number that is not in the array, and returns it. Here is jsfiddle ...

I want to make a function that takes an array as an argument, creates a random number from 1 - 10, and runs until it creates a number that is not in the array, and returns it. Here is jsfiddle ...

i am trying to submit a form via Jquery when change event occurs in select field like this: $('select#slc_level').change(function(event){ event.preventDefault(); console.log('changed') $('form#...

i am trying to submit a form via Jquery when change event occurs in select field like this: $('select#slc_level').change(function(event){ event.preventDefault(); console.log('changed') $('form#...

  1. submit form jquery ajax
  2. submit form jquery post
  3. submit form jquery button click
  4. submit form jquery validation
  5. submit form jquery not working
  6. submit form jquery php
  7. submit form jquery preventdefault
  8. submit form jquery with parameters
  9. submit form jquery without ajax
  10. submit form jquery on click
  11. submit form jquery laravel
  12. submit form jquery without refreshing
  13. submit form jquery stack overflow
  14. submit form jquery ajax asp.net
  15. submit form jquery get response
  16. submit form jquery
  17. submit form jquery ajax php mysql
  18. submit form jquery mvc
  19. submit form jquery asp.net mvc
  20. submit form jquery ajax codeigniter

I would like to repeat a text for 2 seconds in a while loop. How do I break the loop after 2 seconds? This is what I have tried so far but it doesn't work: var repeat = true; setTimeout(function() {...

I would like to repeat a text for 2 seconds in a while loop. How do I break the loop after 2 seconds? This is what I have tried so far but it doesn't work: var repeat = true; setTimeout(function() {...

There is no overflow property for parent elements. The parent element has a set height. The navigation bar simply won't be sticky no matter what I try. It doesn't work with JavaScript either. I must ...

There is no overflow property for parent elements. The parent element has a set height. The navigation bar simply won't be sticky no matter what I try. It doesn't work with JavaScript either. I must ...

  1. isn't
  2. https://www.instagram.com/
  3. isnetworld
  4. instagram
  5. isn't she lovely
  6. isnt it romantic
  7. isna
  8. isn't it ironic
  9. isner
  10. isnull sql
  11. isn network
  12. isnac 2020
  13. isnt she lovely chords
  14. isns
  15. isnan
  16. isn't it
  17. isnull
  18. isn't it romantic cast
  19. isna excel
  20. disney plus