所以我想按ID查找特定文档,例如
creat_acount_button=(Button)findViewById(R.id.creat_acountID);
creat_acount_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name=username.getText().toString().trim();
String em=email.getText().toString().trim();
String pass=password.getText().toString().trim();
final String phoneNum=phone.getText().toString().trim();
final String loc=location.getText().toString().trim();
final String desc=description.getText().toString().trim();
if (!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(loc)&&!TextUtils.isEmpty(em)&&!TextUtils.isEmpty(pass)&&!TextUtils.isEmpty(desc))
{
mProgress.setMessage("Creating Acount");
mProgress.show();
mAuth.createUserWithEmailAndPassword(em,pass)
.addOnSuccessListener(new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
if (authResult!=null)
{
String userid=mAuth.getCurrentUser().getUid();
DatabaseReference curentuserDB=mDatabaseReference.child(userid);
curentuserDB.child("UserName").setValue(username);
curentuserDB.child("phone").setValue(phoneNum);
curentuserDB.child("location").setValue(loc);
curentuserDB.child("Description").setValue(desc);
mProgress.dismiss();
Intent intent = new Intent(Register.this,Post.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
});
}
}
});
上面我有一个学生列表,我想做一个get方法以按ID查找特定的人,例如,说我想找到这个
[
{
"_id": "5d0381ad681a2a3aa1dc5872",
"role_num": 1,
"first_name": "Adilf",
"last_name": "Adli",
"marks": 97
},
{
"_id": "5d0381ad681a2a3aa1dc5873",
"role_num": 2,
"first_name": "Mario",
"last_name": "Adl",
"marks": 93
},
{
"_id": "5d0381ad681a2a3aa1dc5874",
"role_num": 3,
"first_name": "Adi",
"last_name": "Ad",
"marks": 89
},
{
"_id": "5d0381ad681a2a3aa1dc5875",
"role_num": 4,
"first_name": "Harold",
"last_name": "Nome",
"marks": 78
},
{
"_id": "5d10b6124adac42c4467cb88",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d10b65f4adac42c4467cb89",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d10ebc562562921883741a1",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d11af3a62562921883741a3",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d11b40b62562921883741a4",
"role_num": 6,
"first_name": "Mohammed",
"last_name": "Khan",
"marks": 91
},
{
"_id": "5d11b4cd6dc8db0fe0f489fc",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b5131beb7f338414bc96",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b56cd0baff00cc616ebb",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b586d76c463d30d02355",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b74effe56935d88d910e",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b7d1e425ab1dbceda262",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d11b90e44b2c61650377951",
"role_num": 7,
"first_name": "Masrur",
"last_name": "Ahmed",
"marks": 81
},
{
"_id": "5d120883a2f4d83bac8441c7",
"first_name": "Maria"
},
{
"_id": "5d1208cca2f4d83bac8441c8",
"first_name": "Maria"
},
{
"_id": "5d12093fa2f4d83bac8441c9",
"first_name": "Maria"
},
{
"_id": "5d1209c0a2f4d83bac8441ca",
"first_name": "Baki"
},
{
"_id": "5d120f0ed9e07909a413cd85",
"role_num": "5",
"first_name": "Arojfsdof",
"last_name": "adfjaf",
"marks": "12"
},
{
"_id": "5d12fff7d9e07909a413cd8a",
"role_num": "17",
"first_name": "BARKS",
"last_name": "DUDUE",
"marks": "45"
},
{
"_id": "5d131c42313e183ae09966c3",
"role_num": "4",
"first_name": "Maam",
"last_name": "Alsad",
"marks": "80"
}
]
这是我的查找ID的代码,让我知道我必须解决的问题
{
"_id": "5d131c42313e183ae09966c3",
"role_num": "4",
"first_name": "Maam",
"last_name": "Alsad",
"marks": "80"
}
在执行findOne方法之前,该方法仅用于查找第一个文档的出现,而这并不是我要查找的内容
答案 0 :(得分:0)
要从Mongo获取具有其ID的记录,db.collection('students')。findOne({“ _ id:”:id})是正确的格式。您将只能提取一条记录,因为您不能拥有2条具有相同ID的记录。
答案 1 :(得分:0)
缺少new
var ObjectID = require('mongodb').ObjectID;
db.collection('students').find({"_id": new ObjectID(req.body._id)})