0

I'm using this php package to make queries - https://github.com/jenssegers/laravel-mongodb

The situation is, there are two fields, user_id and post_status among others. I want to retrieve all the documents in that collection, but when post_status field value is draft, that should be retrieved only when user_id is a given string. The idea is, only logged in user finds their drafted posts among other posts.

I'm having hard time finding any solution for this problem. The app is still not in production. If I should store data is some different manner, that is an option as well.

  • what is the schema, can you post a record here for better understanding and also what query have you tried? – krishna Prasad Apr 22 '19 at 14:46
  • ``{ "_id" : ObjectId("5cbb6405c393850b88002f63"), "title" : "some title", "status" : "draft", "summary" : null, "post_category" : [ "5c377074c393852988004bad", "5c377090c393852988004bb0" ], "likes_count" : 1, "liked_by" : [ "5c8e3817c3938504b8005f86" ], "views_count" : 0, "comments_count" : 0, "user_id" : "5c618615903aaa496d129d90", "rating" : null, "enabled" : "enabled", "updated_at" : ISODate("2019-04-20T23:23:30.000Z"), "created_at" : ISODate("2019-04-20T18:25:09.000Z") }`` – Mehedi Hasan Nahid Apr 22 '19 at 14:51
  • I want users to find posts with ``"status": "draft"`` among the other posts IF ``user_id`` is their user id. In this case I have no idea what to try as it requires me to run an IF statement in the query. – Mehedi Hasan Nahid Apr 22 '19 at 14:54
  • Your sentence is not clear, what does it mean `among the other posts IF user_id is their user id` does it not mean that all post of a user where `user_id = ` and `status = draft`? – krishna Prasad Apr 22 '19 at 16:56

1 Answers1

0

Find documents with a certain field value only when another field value is a given string

The question your are framing is simply convert into a and query, how let's see it

when another field value is a given string

This means that you have some result sets and you need to filter out when user_id match with some string. i.e some result sets and user_id = <id>

Now consider the first part of the sentence Find documents with a certain field value

This means you are filtering the records with some values i.e "status" = "draft" and whatever result will come and want again to filter on the basis of user_id = <id>

So finally you will end-up with below query:

db.collectionName.find({"status":"draft", "user_id": "5c618615903aaa496d129d90"})

Hope this explanation will help you out or you can rephrase your question I will try to modify by ans.

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
  • Sorry if my phrasing was not very clear. I wasn't so sure how to phrase the questing. I found this question with the same problem as mine and managed to find the solution there. https://stackoverflow.com/questions/25713546/query-mongodb-for-conditional-conditions – Mehedi Hasan Nahid Apr 23 '19 at 01:41
  • ``var usr = "5c618615903aaa496d129d90" db.getCollection('posts').find({ $or:[ {status: {$ne: "draft"}}, {user_id: usr} ]})`` – Mehedi Hasan Nahid Apr 23 '19 at 01:42
  • With the jenssegers package: ``$query = DB::collection('posts')->where(function($qry) use ($user){ $qry->where('user_id', $user) ->orWhere('status', '<>', 'draft'); })->get();`` – Mehedi Hasan Nahid Apr 23 '19 at 01:45