I have the following code that according to user information of a HTML form looks for a record in the MongoDB database:
# -*- coding: utf-8 -*-
import json
from pymongo import MongoClient
Conector = MongoClient('localhost:27017')
Usuario_dato = REQUEST
dato = str(Usuario_dato['find'])
print dato
Datos = Conector['CRUD']
Tabla = Datos['usuarios']
Consulta = Tabla.find({
"$or":[
{
"idUsuario": '/'+dato+'/' #también he intentado { "$regex": /dato/ >>> Falla, '/dato/' >>> también falla }
},
{
"nombreUsuario":'/'+dato+'/'
},
{
"emailUsuario":'/'+dato+'/'
},
{
"telefonoUsuario":'/'+dato+'/'
},
{
"edadUsuario":'/'+dato+'/'
}
]
})
Recopilado = []
for e in Consulta:
Pre_Recopilado = {}
for x in e:
Pre_Recopilado[str(x)] = str(e[x])
Recopilado.append(Pre_Recopilado)
print json.dumps(Recopilado)
User information arrives at User_data through REQUEST
A data I assign the value that interests me to the form sent, in this case it is find :
<input type="text" name="find">
In Data and Table the connection to the CRUD database and the users
Query contains the query to be made, and this is what fails.
In the database I have:
/* 1 */
{
"_id" : ObjectId("59b9a5837a264d076dd40a12"),
"idUsuario" : "1093796198",
"nombreUsuario" : "Martín Bermúdez",
"emailUsuario" : "[email protected]",
"telefonoUsuario" : "3002500989",
"edadUsuario" : "19"
}
/* 2 */
{
"_id" : ObjectId("59ba96d47a264d076dd43177"),
"idUsuario" : "2135130062312",
"nombreUsuario" : "Sofia Gonzales",
"emailUsuario" : "[email protected]",
"telefonoUsuario" : "3205623215",
"edadUsuario" : "26"
}
The situation: