Laravel-mongodb: Find in array value

Created on 3 Jan 2015  路  9Comments  路  Source: jenssegers/laravel-mongodb

At my model there's a field that represents an array of values, actually are tags of my record, for example:

{
    "_id" : ObjectId("54a72f18be9369902900002b"),
    "title" : "First record",
    "tags" : ["tag1",  "tag2", "tag3"],
    "updated_at" : ISODate("2015-01-02T23:51:52.000Z"),
    "created_at" : ISODate("2015-01-02T23:51:52.000Z")
},
{
    "_id" : ObjectId("54a72f18be93699029000029"),
    "title" : "Second record",
    "tags" : ["tag1",  "tag4", "tag5"],
    "updated_at" : ISODate("2015-01-02T23:51:52.000Z"),
    "created_at" : ISODate("2015-01-02T23:51:52.000Z")
},
{
    "_id" : ObjectId("54a72f18be9369902900002a"),
    "title" : "my record",
    "tags" : ["tag6",  "tag7", "tag8"],
    "updated_at" : ISODate("2015-01-02T23:51:52.000Z"),
    "created_at" : ISODate("2015-01-02T23:51:52.000Z")
}

I would retrieve all record that has in "tags" the value "tag1".
It's possible?

Sorry if it was a noob question :P

Most helpful comment

Hey guys, sorry for my mistake... The solutions was in documentation https://github.com/jenssegers/laravel-mongodb#mongodb-specific-operators

I use ::where('tags', 'all', ['campo'])->get() almost equals @girokon suggest.

Thank you all

All 9 comments

Hi,

Not sure if you've found your solution but just in case, that's how I'd do it :

::where('tags.tag1', 'exists', true)->get();

hope it helps.

No, I'm not found solution yet. I'll try your suggestion, tks!

tags is array of strings, so we can do this:

::where("tags","=","tag1") //find all elements where tags has "tag1" or where tags is equals "tag1"
::where("tags","=",["tag1","tag2"]) //find all elmentes where tags is equals ["tag1","tag2"]

Read about query in mongodb documentation http://docs.mongodb.org/manual/tutorial/query-documents/#arrays

Hi guys, unfortunately the both @vainsoul59 and @girokon way doesn't worked for me. Maybe this drive doesn't not support this operation.

I've got the follow error:

{    
  "error": {        
    "type": "ErrorException",        
    "message": "Object of class Jenssegers\Mongodb\Eloquent\Builder could not be converted to string",        
    "file": "C:\Projects\Laminapp\app\service\vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\Response.php",        
    "line": 406    
  }
}

Hey guys, sorry for my mistake... The solutions was in documentation https://github.com/jenssegers/laravel-mongodb#mongodb-specific-operators

I use ::where('tags', 'all', ['campo'])->get() almost equals @girokon suggest.

Thank you all

If the tags are integer, don't forget to typecast!
Coupon::where('recipients', 'all', [(int)$cid])->get();
(I took half day to find it!)

@Saneesh thanks for you comment, its works !

The solutions was in documentation https://docs.mongodb.com/manual/tutorial/query-arrays/

The following example queries for all documents where tags is an array that contains the string "tag1" as one of its elements:
db.inventory.find( { tags: "tag1" } )

so, in laravel, you just do this:
::where('tags', 'tag1')

How do you find any of the array given, or just in common language that "contains any of those values"?
Like if you want to get the docs containing tag1 and also the ones containing tag8?
::where('tags', 'all', ['tag1', 'tag8'])->get() does not work : you will get only the docs containing _both_ tag1 and tag8.

Is there anything like: ::where('tags', 'any', ['tag1', 'tag8'])->get() ?

Edit, solution found, just below in case it could help anyone else:
::whereIn('tags', ['tag1', 'tag8'])->get()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanjay1688 picture sanjay1688  路  3Comments

naveedyasin picture naveedyasin  路  3Comments

BlakeGardner picture BlakeGardner  路  3Comments

HassanIbrahim picture HassanIbrahim  路  3Comments

ricardofontanelli picture ricardofontanelli  路  3Comments