Ich erhalte eine Fehlermeldung, wenn ich versuche, eine Sammlung in PHP zu aggregieren. Collection "lbls2" enthält Dokumente, die wie folgt aussehen:Ausführen von MongoDB-Aggregatoperation in PHP
{
"_id" : ObjectID(1234),
"values" : ["String 1", "String 2", "String 3"]
}
{
"_id" : ObjectID(5678),
"values" : ["String 1", "String 4", "String 7"]
}
{
"_id" : ObjectID(9101),
"values" : ["String 3", "String 5", "String 10"]
}
Als ich db.lbls2.aggregate({'$project' : {'values' : 1, '_id' : 0}})
im Mongo Shell ausgeführt, gibt sie:
{
"values" : [
"String 1",
"String 2",
"String 3"
]
}
{
"values" : [
"String 1",
"String 4",
"String 7"
]
}
{
"values" : [
"String 3",
"String 5",
"String 10"
]
}
Wenn ich $r = $c_l->aggregate(['$project' => ['values' => 1, '_id' => 0]]);
in PHP laufen, gibt sie:
Schwerwiegender Fehler: Die nicht abgefangene Ausnahme 'MongoDB \ Exception \ InvalidArgumentException' mit der Nachricht '$ pipeline ist keine Liste (unerwarteter Index: "$ project")' in C: \ xampp \ htdocs \ gc4 \ vendor \ mongodb \ mongodb \ src \ Operation \ Aggregate.php: 93 Stack-Trace: # 0 C: \ xampp \ htdocs \ gc4 \ Hersteller \ mongodb \ mongodb \ src \ Collection.php (186): MongoDB \ Vorgang \ Aggregat -> __ Konstrukt ('gc_dev', 'llbs2', Array, Array) # 1 C: \ xampp \ htdocs \ gc4 \ admin_dashboard.php (9): MongoDB \ Collection-> Aggregat (Array) # 2 {main} geworfen in C: \ xampp \ htdocs \ gc4 \ Hersteller \ mongodb \ mongodb \ src \ Operation \ Aggregate. php on line 93
Interessanterweise gibt db.lbls2.aggregate({'$project' : {'values' : '$values.0', '_id' : 0}})
(projecting values index 0) '{"labels": []} zurück, also ist es möglich, dass ich die Insertion vermasselt habe oder etwas Seltsames gemacht habe. Was mache ich falsch?
Hier ist mein PHP-Skript: require ('./vendor/autoload.php');
$client = new MongoDB\Client("mongodb://localhost:27017");
$db = $client->gc_dev;
$c_l = $db->lbls2;
$r = $c_l->aggregate(['$project' => ['values' => 1, '_id' => 0]]);
Und hier ist Teil der Python-Skript ich die Werte einfügen verwendet:
a = urllib2.urlopen('http://urlredacted.com')
a = bs(a)
length = len(a.findAll('a',id=re.compile('rptResults_*')))
for row in a.findAll('a',id=re.compile('rptResults_*')):
cells = row.get_text()
labels.append(cells)
lastIndex =len(labels)
for i in range (0,lastIndex):
labels[i] = unicodedata.normalize('NFKD',labels[i]).encode('ascii','ignore')
#connect to db and insert data
client = MongoClient("mongodb://localhost:27017")
db = client.gc_dev
c_lbls = db.lbls2
lbls_insert = {
"values" : labels
}
c_lbls.insert_one(lbls_insert)