MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Corrispondenza con la sottostringa nell'aggregazione mongodb

Penso che tu stia cercando di eseguire query utilizzando il framework di aggregazione poiché hai provato gli operatori $match e $substr. Ho creato un semplice esempio per mostrare come puoi usare $substr per ottenere il risultato che volevi sul framework di aggregazione.

Ho inserito i seguenti dati nel MongoDB.

{ "_id" : ObjectId("528b343881d4fe2cfe0b1b25"), "time_stamp" : "2013-06-30 23:58:37 928" }
{ "_id" : ObjectId("528b343b81d4fe2cfe0b1b26"), "time_stamp" : "2013-06-30 23:58:37 928" }
{ "_id" : ObjectId("528b344c81d4fe2cfe0b1b27"), "time_stamp" : "2013-06-30 12:58:37 928" }
{ "_id" : ObjectId("528b344f81d4fe2cfe0b1b28"), "time_stamp" : "2013-06-30 12:58:23 928" }
{ "_id" : ObjectId("528b345381d4fe2cfe0b1b29"), "time_stamp" : "2013-06-31 12:58:23 928" }
{ "_id" : ObjectId("528b345981d4fe2cfe0b1b2a"), "time_stamp" : "2013-07-31 12:58:23 933" }

Ho scritto il seguente codice per raggruppare per data utilizzando l'operatore $substr.

db.myObject.aggregate(
{$project : {new_time_stamp : {$substr : ["$time_stamp",0, 10]}}},
{$group:{_id:"$new_time_stamp", "count": {$sum:1}}}
);