[英]How to make embedded queries with many to many relations in mongodb
我有这些关系: https : //pastebin.com/hMZRJe0S
我需要得到用户的ui
使用java脚本我试图得到usuario的id,然后是rol的id,然后是funcion的id,最后是ius。 通过获取匹配元素然后将id转换为数组以获取下一个表(或集合)的元素
狐狸的例子这就是我如何获得rol的ID:
const dbo = db.db("tareas");
dbo
.collection("usuario")
.find({ nombre: name, password })
.toArray(function(err, usuarios) {
console.log(2);
if (err) throw err;
console.log(usuarios);
if (usuarios.length > 0) {
var x;
dbo
.collection("usuario_rol")
.find({ "_id.id_usuario": usuarios[0]._id })
.toArray(function(err, usuarios_rol) {
if (err) throw err;
var idroles = [];
for (x = 0; x < usuarios_rol.length; x++) {
idroles.push(usuarios_rol[x]._id.id_rol);
}
然后我得到这个“拓扑被摧毁”
C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ utils.js:132 throw err; ^
MongoError:在nextFunction(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules)的initializeCursor(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:596:25)中销毁了拓扑\\ mongodb-core \\ lib \\ cursor.js:456:12)在Cursor的Cursor.next(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb-core \\ lib \\ cursor.js:766:3)。在fetchDocs的_next(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ cursor.js:216:36)(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ operations \\ cursor_ops .js:217:12)在executeOperation上的toArray(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ operations \\ cursor_ops.js:247:3)(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ utils.js:416:24)位于C:\\ Users的Cursor.toArray(C:\\ Users \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ cursor.js:829:10) \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ routes \\ api \\ members.js:75:18结果(C:\\ U) sers \\ NORMA \\ Desktop \\ EXPRESS_CRASH_COURSE \\ node_modules \\ mongodb \\ lib \\ utils.js:410:17)[nodemon] app崩溃 - 在开始之前等待文件更改...
Try with this code working fine getting result
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
MongoClient.connect(url, function(err, db) {
name="juan";
password="juan123"
console.log(1);
if (err) throw err;
const dbo = db.db("tareas");
dbo
.collection("usuario")
.find({ nombre: name, password })
.toArray(function(err, usuarios) {
console.log(2);
if (err) throw err;
console.log(usuarios);
if (usuarios.length > 0) {
var x;
dbo
.collection("usuario_rol")
.find({ "_id.id_usuario": usuarios[0]._id })
.toArray(function(err, usuarios_rol) {
if (err) throw err;
var idroles = [];
for (x = 0; x < usuarios_rol.length; x++) {
idroles.push(usuarios_rol[x]._id.id_rol);
}
console.log(idroles);
dbo
.collection("rol_funcion")
.find({ "_id.id_rol": { $in: idroles } })
.toArray(function(err, roles_fun) {
if (err) throw err;
var idfunciones = [];
console.log(roles_fun);
for (x = 0; x < roles_fun.length; x++) {
idfunciones.push(roles_fun[x]._id.id_funcion);
}
dbo
.collection("funcion_iu")
.find({ "_id.id_funcion": { $in: idfunciones } })
.toArray(function(err, fun_iu) {
if (err) throw err;
var idius = [];
for (x = 0; x < fun_iu.length; x++) {
idius.push(fun_iu[x]._id.id_iu);
}
dbo
.collection("iu")
.find({ _id: { $in: idius } })
.toArray(function(err, ius) {
if (err) throw err;
console.log(ius);
});
});
});
});
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.