Error aggregate $match $and

I have this error:

db.datos_sensores.aggregate([{$project:{timestamp:{$dateFromString:{dateString:‘$timestamp’}}},“_id”:0, “medidas”:{$slice:[“$medidas”,-1]},“location_id”:1}, {$addFields:{Hora:{$hour:“$timestamp”},Diadelasemana:{$dayOfWeek:“$timestamp”}}}, {$match:{‘Diadelasemana’:{$in:[‘1’,‘7’]},$and:[{‘Diadelasemana’:‘6’},{‘Hora’:{$gte:16}}],$and:[{‘Diadelasemana’:‘2’},{‘Hora’:{$lt:‘8’}}]}}])
uncaught exception: Error: command failed: {
“ok” : 0,
“errmsg” : “A pipeline stage specification object must contain exactly one field.”,
“code” : 40323,
“codeName” : “Location40323”
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:618:17
assert.commandWorked@src/mongo/shell/assert.js:708:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12
@(shell):1:1

Could you help me?

In cases like this where you’re getting an error from a .aggregate() call, the first thing to do while troubleshooting it to format your query so you can easily see how the brackets line up. Your query lines up as follows:

db.datos_sensores.aggregate(
    [
        {
            $project:{
                timestamp:{
                    $dateFromString:{
                        dateString:’$timestamp’
                    }
                }
            },
            "_id":0, 
            "medidas":{
                $slice:["$medidas",-1]
            },
            "location_id":1
        }, 
        {
            $addFields:{
                Hora:{
                    $hour:"$timestamp"
                },
                Diadelasemana:{
                    $dayOfWeek:"$timestamp"
                }
            }
        }, 
        {
            $match:{
                ‘Diadelasemana’:{
                    $in:[‘1’,‘7’]
                },
                $and:[
                    {‘Diadelasemana’:‘6’},
                    {‘Hora’:{$gte:16}}
                ],
                $and:[
                    {‘Diadelasemana’:‘2’},
                    {‘Hora’:{$lt:‘8’}}
                ]
            }
        }
    ]
)

Notice that there is an extra closing brace in your $project so a lot of the files for projection have been moved outside that stage. This has also thrown everything else off.

1 Like

If we try to reformat your code to understand

[
  { $project :
    {
      timestamp : { $dateFromString:{dateString:’$timestamp’}}
    } ,
    "_id":0,
    “medidas” : {$slice:["$medidas",-1]},
    “location_id”:1
  },
  { $addFields :
    {
      Hora:{$hour:"$timestamp"},
      Diadelasemana:{$dayOfWeek:"$timestamp"}
    }
  },
  { $match:
    {
      ‘Diadelasemana’:{$in:[‘1’,‘7’]},
      $and:[{‘Diadelasemana’:‘6’},{‘Hora’:{$gte:16}}],
      $and:[{‘Diadelasemana’:‘2’},{‘Hora’:{$lt:‘8’}}]
    }
  }
]

we can see the you are closing your $project too soon.

Read Formatting code and log snippets in posts before posting code or documents. It is hard to help when the markup is wrong and all quotes are screwed up.

Using variables for stages help in finding errors, it is adaptation of Divide And Conquer. For example:

mongosh> match_stage =  {$match:{Diadelasemana:{$in:[1,7]},$and:[{Diadelasemana:6},{Hora:{$gte:16}}],$and:[{Diadelasemana:2},{Hora:{$lt:8}}]}}
/* produce the output */
{$match:{Diadelasemana:{$in:[1,7]},$and:[{Diadelasemana:2},{Hora:{$lt:8}}]}}

Because with JSON you cannot have an object with duplicate key. Well, you can but you only get the last occurrence. Anyway in this case the logic is wrong since you cannot have
both Diadelasemana:2 and Diadelasemana:6 both true.

Sorry for reply the almost the same at almost the same time.

Why you write what you wrote and then delete it. I feel you are, if not smarter then more knowledgeable (if we can say than in English) than me.

I deleted my post because it was not correct. Not the part about you being smarter than me (I do feel this is correct and I’ve learned a lot from you over the years here), but the part about my having the brackets incorrect.

Ditto over here. Every post is bringing some light.

1 Like

Thanks a lot guys. I have got to work fine!!

3 Likes