sudhanva
sudhanva

Reputation: 709

Projecting array values in mongo document based on non array fields

The data in my database is as follows:

/* 1 */
{
      "name": "Severus",
      "u_name": "severussnape",
      "info": [
        {
          "value": "Severus",
          "source": "1",
          "infotag": "name"
        },
        {
          "value": "severussnape",
          "source": "2",
          "infotag": "name"
        }
      ]
}

/* 2 */
{
      "name": "Harry",
      "u_name": null,
      "info": [
        {
        "value": "Harry",
          "source": "1",
          "infotag": "name"
        }
      ]
}

I'd like to project the data so that the name field and the info array in the result are changed based on whether or not the u_name field is null. The result I expect looks like so:

/* 1 */
{
      "name": "severussnape",
      "info": [
        {
          "value": "severussnape",
          "source": "2",
          "infotag": "name"
        }
      ]
}

/* 2 */
{
      "name": "Harry",      
      "info": [
        {
          "value": "Harry",
          "source": "1",
          "infotag": "name"
        }
      ]
}

I've managed to project the name field correctly using the projection:

db.database.aggregate([{
    $project:{
        "name":{$cond:[{$eq:["$u_name",null]},"$name","$u_name"]}
    }
}])

But I've been unable to figure out how to remove the array element from info with value "Severus" in the first document depending on u_name. Is this possible?

Upvotes: 0

Views: 41

Answers (3)

Vikash_Singh
Vikash_Singh

Reputation: 1896

You should try this aggregate query, this will solve your problem.

db.database.aggregate([
    {
        $project:{
            'name': {
                '$ifNull': ['$u_name', '$name']
            },
            'info': '$info'
        }
    },
    {
        $project: {
            'name': '$name',
            'info': {
                '$filter': {
                    'input': '$info',
                    'as': 'info',
                    'cond': {
                        '$eq':['$$info.value', '$name']
                    }
                }
            }
        }
    }
])

Upvotes: 1

Ashwanth Madhav
Ashwanth Madhav

Reputation: 1134

Is this helpful ?

if any condition need. u can match it.

db.getCollection('TestQueries').aggregate([
{$unwind:{
 path:"$info",
 preserveNullAndEmptyArrays:true
}},
{$project:{
  name:"$info.value",
  info:1
}}
])

Upvotes: 0

Jitendra
Jitendra

Reputation: 3185

You can try something like below:

db.database.aggregate([
    {
        $project:{
            "name":{ $cond: { if: { $eq:["$u_name",null] }, then: "$name", else: "$u_name" } },
        }
    },
])

Upvotes: 0

Related Questions