我在Mongodb工作的文件:
{ "_id" : 3, "sid" : 2, "name" : "A", "score" : 60 }
{ "_id" : 5, "sid" : 3, "name" : "B", "score" : 50 }
{ "_id" : 2, "sid" : 1, "name" : "C", "score" : 44 }
{ "_id" : 1, "sid" : 0, "name" : "D", "score" : 14 }
{ "_id" : 4, "sid" : 4, "name" : "E", "score" : 28 }
{ "_id" : 8, "sid" : 3, "name" : "B", "score" : 92 }
{ "_id" : 6, "sid" : 0, "name" : "D", "score" : 63 }
{ "_id" : 9, "sid" : 4, "name" : "E", "score" : 5 }
{ "_id" : 10, "sid" : 6, "name" : "F", "score" : 81 }
{ "_id" : 11, "sid" : 5, "name" : "G", "score" : 23 }
{ "_id" : 13, "sid" : 8, "name" : "H", "score" : 67 }
{ "_id" : 14, "sid" : 6, "name" : "F", "score" : 89 }
{ "_id" : 12, "sid" : 9, "name" : "I", "score" : 75 }
{ "_id" : 15, "sid" : 7, "name" : "J", "score" : 85 }
{ "_id" : 7, "sid" : 1, "name" : "C", "score" : 21 }
{ "_id" : 16, "sid" : 9, "name" : "I", "score" : 16 }
{ "_id" : 17, "sid" : 8, "name" : "H", "score" : 66 }
{ "_id" : 19, "sid" : 7, "name" : "J", "score" : 63 }
{ "_id" : 18, "sid" : 2, "name" : "A", "score" : 97 }
{ "_id" : 20, "sid" : 5, "name" : "G", "score" : 41 }
我有一个变量" low",包含每个学生的较低分数:
{ "name" : "F", "score" : 81 }
{ "name" : "H", "score" : 66 }
{ "name" : "J", "score" : 63 }
{ "name" : "A", "score" : 60 }
{ "name" : "B", "score" : 50 }
{ "name" : "G", "score" : 23 }
{ "name" : "C", "score" : 21 }
{ "name" : "I", "score" : 16 }
{ "name" : "D", "score" : 14 }
{ "name" : "E", "score" : 5 }
我正在尝试检索名称" A"的得分值。并将其更新为65.
我该怎么做?
答案 0 :(得分:0)
假设您在本地上下文中有变量low
,那么您可以像这样使用普通的JavaScript:
> db.coll.find()
{ "_id" : ObjectId("57204c236b86b7f53c0e33fb"), "name" : "Ali", "score" : 81 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fc"), "name" : "Hamid", "score" : 66 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fd"), "name" : "Mo", "score" : 63 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fe"), "name" : "Pat", "score" : 60 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33ff"), "name" : "Joe", "score" : 50 }
{ "_id" : ObjectId("57204c236b86b7f53c0e3400"), "name" : "Kurt", "score" : 23 }
> var low=db.coll.find({}, {_id:0});
> var newarr=new Array();
> low.forEach(function(item){
... if(item.name=="Pat")
... item.score=65;
... newarr.push(item);
... });
> newarr
[
{
"name" : "Ali",
"score" : 81
},
{
"name" : "Hamid",
"score" : 66
},
{
"name" : "Mo",
"score" : 63
},
{
"name" : "Pat",
"score" : 65
},
{
"name" : "Joe",
"score" : 50
},
{
"name" : "Kurt",
"score" : 23
}
]
>
如果您想要更新到集合中,可以使用:
> db.coll.find()
{ "_id" : ObjectId("57204c236b86b7f53c0e33fb"), "name" : "Ali", "score" : 81 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fc"), "name" : "Hamid", "score" : 66 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fd"), "name" : "Mo", "score" : 63 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fe"), "name" : "Pat", "score" : 60 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33ff"), "name" : "Joe", "score" : 50 }
{ "_id" : ObjectId("57204c236b86b7f53c0e3400"), "name" : "Kurt", "score" : 23 }
> db.coll.update({name:"Pat"}, {$set: {score:65}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.coll.find()
{ "_id" : ObjectId("57204c236b86b7f53c0e33fb"), "name" : "Ali", "score" : 81 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fc"), "name" : "Hamid", "score" : 66 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fd"), "name" : "Mo", "score" : 63 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33fe"), "name" : "Pat", "score" : 65 }
{ "_id" : ObjectId("57204c236b86b7f53c0e33ff"), "name" : "Joe", "score" : 50 }
{ "_id" : ObjectId("57204c236b86b7f53c0e3400"), "name" : "Kurt", "score" : 23 }
>