MongoDBの値を1つだけデクリメントしますか?
まず、ドキュメントを使用してコレクションを作成しましょう-
>db.decrementingOperationDemo.insertOne({"ProductName":"Product-1","ProductPrice":756});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7a8ae6d78f205348bc63c")
}
>db.decrementingOperationDemo.insertOne({"ProductName":"Product-2","ProductPrice":890});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7a8b86d78f205348bc63d")
}
>db.decrementingOperationDemo.insertOne({"ProductName":"Product-3","ProductPrice":994});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7a8c66d78f205348bc63e")
}
>db.decrementingOperationDemo.insertOne({"ProductName":"Product-4","ProductPrice":1000});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd7a8d06d78f205348bc63f")
} 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.decrementingOperationDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd7a8ae6d78f205348bc63c"),
"ProductName" : "Product-1",
"ProductPrice" : 756
}
{
"_id" : ObjectId("5cd7a8b86d78f205348bc63d"),
"ProductName" : "Product-2",
"ProductPrice" : 890
}
{
"_id" : ObjectId("5cd7a8c66d78f205348bc63e"),
"ProductName" : "Product-3",
"ProductPrice" : 994
}
{
"_id" : ObjectId("5cd7a8d06d78f205348bc63f"),
"ProductName" : "Product-4",
"ProductPrice" : 1000
} 以下は、単一の値をデクリメントするクエリです-
> db.decrementingOperationDemo.update({_id: ObjectId("5cd7a8d06d78f205348bc63f"), ProductPrice: {$gt: 0}}, {$inc: {ProductPrice: -10}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) もう一度ドキュメントを確認しましょう-
> db.decrementingOperationDemo.find().pretty();
これにより、次の出力が生成されます-
{
"_id" : ObjectId("5cd7a8ae6d78f205348bc63c"),
"ProductName" : "Product-1",
"ProductPrice" : 756
}
{
"_id" : ObjectId("5cd7a8b86d78f205348bc63d"),
"ProductName" : "Product-2",
"ProductPrice" : 890
}
{
"_id" : ObjectId("5cd7a8c66d78f205348bc63e"),
"ProductName" : "Product-3",
"ProductPrice" : 994
}
{
"_id" : ObjectId("5cd7a8d06d78f205348bc63f"),
"ProductName" : "Product-4",
"ProductPrice" : 990
} -
MySQLで単一の列値のみを更新しますか?
まずテーブルを作成しましょう- mysql> create table DemoTable770 ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value int ); Query OK, 0 rows affected (0.65 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable770(Value) values(10); Query OK, 1 row affected (0.16 sec) mysql> insert into
-
MySQLの単一の列値のみを更新します
まずテーブルを作成しましょう- mysql> create table DemoTable1605 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentCountryName varchar(20) -> ); Query OK, 0 rows affected (0.48 sec) 挿入