我想Mongo database
在year
明智的文档中使用PHP脚本插入数据,以便它看起来像这样(所有年份都在一个文档中);
cars{
2017{
car=Motorolla
color = blue
}
2016{
car=Toyota
color = green
}
2015{
car=Corolla
color = black
}
}
Run Code Online (Sandbox Code Playgroud)
我想添加文档,但它提示
文档不能有$前缀字段名称:$ years [0]
是否可以使用PHP在Mongo中创建这样的模式?
码
<?php
try {
$car = 'Motorolla';
$color = 'blue';
//$car = 'Toyota';
//$color = 'green';
//$car = 'Corolla';
//$color = 'black';
$years = array(2017, 2016, 2015);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
$document = ['_id' => new MongoDB\BSON\ObjectID, '$years[0]' => $car, '$years[1]' => $color]; // Making a query type
try {
$bulkWriteManager->insert($document); // Inserting Document
echo 1;
} catch(MongoCursorException $e) {
/* handle the exception */
echo 0;
}
$manager->executeBulkWrite('dbName.carsCol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
我不想一次添加整车对象.我想Year object
每次都添加.任何帮助都会很明显.
或
任何相关答案,以便我可以Mongo Database
根据年份获得数据?
EDIT1
第一次创作. - 积分转到@Veeram
<?php
try {
$car = 'Malibu';
$color = 'red';
$years = array(2017);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
//{"car":"chevy", "color":"black", year: 2017}
$insert = ['car' => $car, 'color' => $color, 'year' => $years[0]];
try {
$bulkWriteManager -> insert($insert); // Inserting Document
echo 1;
} catch (MongoCursorException $e) {
echo 0;
}
$manager->executeBulkWrite('dbName.mycol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
echo "In file:", $e->getFile(), "\n";
echo "On line:", $e->getLine(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
对于更新 - Credits转到@Veeram
<?php
try {
$car = 'ChangedCar';
$color = 'changedColor';
$years = array(2017);
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
$query = ['cars.year' => $years[0]];
//{ $push: { "cars.$.data": { "car":"chevy", "color":"black"} }}
$update = ['$push'=> ['cars.$.data'=>['car' => $car, 'color' => $color]]];
try {
$bulkWriteManager->update($query, $update); // Inserting Document
} catch(MongoCursorException $e) {
}
$manager->executeBulkWrite('dbName.mycol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
此代码中的问题是它第一次成功插入数据,但是当我更新数据时,它不会更新它.
示例:
有一个名为的文档cars
.将包含year的对象的数据插入到一个文档中.假设对象是2017,它包含颜色和汽车模型.如下图所示; (具有年份的多个对象.年份在整个文档中是唯一的.)
cars{
2017{
car=Motorolla
color = blue
}
2016{
car=Toyota
color = green
}
2015{
car=Corolla
color = black
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想更新只是制作一个2017
类似的对象2017{car=Updated-Motorolla color =Updated-blue}
并插入文档中.它应该只更新文档中的2017年对象.
cars{
2017{
car=Updated-Motorolla
color =Updated-blue
}
2016{
car=Toyota
color = green
}
2015{
car=Corolla
color = black
}
}
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样的事情。仅基于键作为值来执行所有 Mongo 数据库操作是不可能的。
第一个解决方案的编写是为了接近 OP 的设计。
假设您可以向year
.
{
"cars": [{
"year": "2017",
"data": [{
"car": "Motorolla",
"color": "blue"
}]
}, {
"year": "2016",
"data": [{
"car": "Toyota",
"color": "green"
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
可以轻松地通过其值引用年份。
例如,要向2017 年data
的数组添加新值year
。您可以尝试以下代码。
使用更新位置$运算符。
query
部分引用存储 2017 年记录的数组。
update
部分用于push
将新car
记录添加到行的现有data
数组中2017
。
<?php
try {
$car = 'Malibu';
$color = 'blue';
$years = [2017];
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
//{"cars.year":2017}
$query = ['cars.year' => $years[0]];
//{ $push: { "cars.$.data": { "car":"chevy", "color":"black"} }}
$update = ['$push'=> ['cars.$.data'=>['car' => $car, 'color' => $color]]];
try {
$bulkWriteManager->update($query, $update); // Update Document
echo 1;
} catch(MongoCursorException $e) {
/* handle the exception */
echo 0;
}
$manager->executeBulkWrite('dbName.carsCol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
要按年份访问数据,您可以运行以下查询。
使用查询位置$
运算符通过查询部分查找数组索引,并在投影部分引用该值。
db.collection.find({"cars.year":2017}, {"cars.$.data":1});
Run Code Online (Sandbox Code Playgroud)
替代解决方案:
这将像插入一样处理一切
您最好将每个汽车条目保存在其自己的文档中。
{ "year" : 2017, "car" : "Motorolla", "color" : "blue" }
{ "year" : 2016, "car" : "Toyota", "color" : "green" }
{ "year" : 2015, "car" : "Corolla", "color" : "black" }
Run Code Online (Sandbox Code Playgroud)
对于每个条目,您可以使用:
db.collection.insert({"year":2017, "car":"Motorolla", "color":"blue"});
Run Code Online (Sandbox Code Playgroud)
PHP代码:
//{"car":"chevy", "color":"black", year: 2017}
$insert = ['car' => $car, 'color' => $color, 'year' => $years[0]];
try {
$bulkWriteManager - > insert($insert); // Inserting Document
echo 1;
} catch (MongoCursorException $e) {
/* handle the exception */
echo 0;
}
Run Code Online (Sandbox Code Playgroud)
要按年份访问数据,您可以使用
db.collection.find({"year":2017});
Run Code Online (Sandbox Code Playgroud)
更新的 PHP 代码:
<?php
try {
$cars = ['Motorolla','Toyota', 'Corolla'] ;
$colors = ['blue', 'green', 'black'];
$years = [2017, 2016, 2015];
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite;
$query1 =["year" => $years[0]];
$query2 =["year" => $years[1]];
$query3 =["year" => $years[2]];
$update1 = ['$set' => ['car' => $cars[0], 'color' => $colors[0]]];
$update2 = ['$set' => ['car' => $cars[1], 'color' => $colors[1]]];
$update3 = ['$set' => ['car' => $cars[2], 'color' => $colors[2]]];
try {
$bulkWriteManager->update($query1, $update1, ["upsert" => true]);
$bulkWriteManager->update($query2, $update2, ["upsert" => true]);
$bulkWriteManager->update($query3, $update3, ["upsert" => true]);
echo 1;
} catch(MongoCursorException $e) {
/* handle the exception */
echo 0;
}
$manager->executeBulkWrite('dbName.carsCol', $bulkWriteManager); // Going to DB and Collection
} catch (MongoDB\Driver\Exception\Exception $e) {
$filename = basename(__FILE__);
echo "The $filename script has experienced an error.\n";
echo "It failed with the following exception:\n";
echo "Exception:", $e->getMessage(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)
您可以使用聚合管道执行复杂的查询,并且可以添加索引以使响应更快。
观察结果:
第一个解决方案:更难更新/插入数据,但将所有内容放在一起,以便更容易读取数据。
第二种解决方案:更干净、更简单地对文档进行 CRUD 操作,并使用聚合管道来执行复杂的查询。