0

I am new to MongoDb. I am using Laravel with Jessengers. In my collection, I have an embed which is an array. I only want to get the documents where the embed actually has values.

I have made several attempts at this with answers I found on SO such as this

MyCollection::where('myEmbed.0', 'exists', true)->get(); and MyCollection::where(count(myEmbed), '>', 0)->get();

None of these work... Can someone tell me what I am doing wrong and what would be the correct approach?

Premisoft
  • 177
  • 12

1 Answers1

0

In mongoDB here is the output from Mongoshell. can you please see if this can be converted to laravel/whatever your program syntax is there? Please note exists is used with a prefix $ sign in mongo, that is not there in your code.

> db.multiArr.find();
{ "_id" : ObjectId("5f4d1fd58379a4e3f957641b"), "ID" : "fruit1", "Keys" : [ [ "apple", "carrot", "banana" ] ] }
{ "_id" : ObjectId("5f4d1fd58379a4e3f957641c"), "ID" : "fruit2", "Keys" : [ [ "apple", "orange", "banana" ] ] }
{ "_id" : ObjectId("5f4dc7d6673d93c7e20e48f6"), "ID" : "fruit3", "Keys" : [ "figs", "dates", "apple" ] }
{ "_id" : ObjectId("5f4dc86d673d93c7e20e48f7"), "ID" : "fruit4", "Keys" : [ ] }
{ "_id" : ObjectId("5f4dc99b673d93c7e20e48f8"), "ID" : "fruit5" }
> db.multiArr.find({Keys:{$exists:true,$ne:[]}});
{ "_id" : ObjectId("5f4d1fd58379a4e3f957641b"), "ID" : "fruit1", "Keys" : [ [ "apple", "carrot", "banana" ] ] }
{ "_id" : ObjectId("5f4d1fd58379a4e3f957641c"), "ID" : "fruit2", "Keys" : [ [ "apple", "orange", "banana" ] ] }
{ "_id" : ObjectId("5f4dc7d6673d93c7e20e48f6"), "ID" : "fruit3", "Keys" : [ "figs", "dates", "apple" ] }
>
Mallik
  • 336
  • 2
  • 6
  • The following post is useful: I learned from here. https://stackoverflow.com/questions/14789684/find-mongodb-records-where-array-field-is-not-empty – Mallik Sep 01 '20 at 04:15
  • That doesn't seem to work for me. I am sure there is a simple way of doing just what I need to do, but can't quite figure out what it is... – Premisoft Sep 01 '20 at 17:29