Javascript 来自环回问题的mongo查询
我试图从环回模型中查询mongo db。但我没有从monogdb得到任何结果 这就是我的文档在mongodb中的外观Javascript 来自环回问题的mongo查询,javascript,node.js,mongodb,loopbackjs,Javascript,Node.js,Mongodb,Loopbackjs,我试图从环回模型中查询mongo db。但我没有从monogdb得到任何结果 这就是我的文档在mongodb中的外观 {"_id":"5b9f8bc51fbd7f248cabe742", "agentType":"Online-Shopping", "projectId":"modroid-server", "labels":["category","price"], "phrases":["Look for _^ct_ in _^p_ ", "I need _^ct_ in _^p_", "c
{"_id":"5b9f8bc51fbd7f248cabe742",
"agentType":"Online-Shopping",
"projectId":"modroid-server",
"labels":["category","price"],
"phrases":["Look for _^ct_ in _^p_ ",
"I need _^ct_ in _^p_",
"can you search for _^ct_ in _^p_"]
}
module.exports = function (Agent) {
Agent.getDataSource().connector.connect(function (err, db) {
var collection = db.collection("text-responses");
collection.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
if(err) {
cb(err);
return;
}
res.toArray(function (err, realRes) { // this part is crucial
// otherwise if you try to print res you will get a dump of the db object
if(err) {
cb(err);
return;
}
console.log("documnet result:"+realRes);
console.log("document err:"+err);
})
})
}
);
}
这是我的数据源文件
{
"db": {
"name": "db",
"connector": "memory"
},
"modroid-server": {
"host": "localhost",
"port": 27017,
"url": "",
"database": "modroid-server",
"password": "",
"name": "modroid-server",
"user": "",
"connector": "mongodb"
}
}
这是模型配置文件
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models"
],
"mixins": [
"loopback/common/mixins",
"loopback/server/mixins",
"../common/mixins",
"./mixins"
]
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false,
"options": {
"strictObjectIDCoercion": true
}
},
"Role": {
"dataSource": "db",
"public": false
},
"agent": {
"dataSource": "modroid-server", // here is my mongodb
"public": true
}
}
这是我在mongodb中查询的代码
{"_id":"5b9f8bc51fbd7f248cabe742",
"agentType":"Online-Shopping",
"projectId":"modroid-server",
"labels":["category","price"],
"phrases":["Look for _^ct_ in _^p_ ",
"I need _^ct_ in _^p_",
"can you search for _^ct_ in _^p_"]
}
module.exports = function (Agent) {
Agent.getDataSource().connector.connect(function (err, db) {
var collection = db.collection("text-responses");
collection.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
if(err) {
cb(err);
return;
}
res.toArray(function (err, realRes) { // this part is crucial
// otherwise if you try to print res you will get a dump of the db object
if(err) {
cb(err);
return;
}
console.log("documnet result:"+realRes);
console.log("document err:"+err);
})
})
}
);
}
当我点击RESTAPI时。我得到这个输出
collection find res:[object Object]
collection find err:null
documnet result:
document err:null
请帮帮我,我哪里做错了。好几天来我一直都是这样
已编辑
当我打印res时,它会给我大量的数据,这些数据从
Cursor {
pool: null,
server: null,
disconnectHandler:
Store {
s: { storedOps: [], storeOptions: [Object], topology: [Object] },
length: [Getter] },
bson: BSON {},
ns: 'modroid-server.text-responses',
cmd:
{ find: 'modroid-server.text-responses',
limit: 0,
skip: 0,
query: { where: [Object] },
slaveOk: true,
readPreference: ReadPreference { mode: 'primary', tags: undefined } },
看起来像
文本响应
不是模型。
module.exports = function (Model) {
Model.getDataSource().connector.connect(function (err, db) {
Model.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
})
});
}
尝试使用直接模型代理。
module.exports = function (Model) {
Model.getDataSource().connector.connect(function (err, db) {
Model.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
})
});
}
或
按收藏分类
module.exports = function (Model) {
Model.getDataSource().connector.connect(function (err, db) {
var collection = db.collection("collection-name"); //use agent
collection.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
})
}
);
}
看起来像
文本响应
不是模型。
module.exports = function (Model) {
Model.getDataSource().connector.connect(function (err, db) {
Model.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
})
});
}
尝试使用直接模型代理。
module.exports = function (Model) {
Model.getDataSource().connector.connect(function (err, db) {
Model.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
})
});
}
或
按收藏分类
module.exports = function (Model) {
Model.getDataSource().connector.connect(function (err, db) {
var collection = db.collection("collection-name"); //use agent
collection.find({ "where": { "labels": ["category", "price"] } }, function (err, res) { // define whichever query you need
console.log("collection find res:"+res);
console.log("collection find err:"+err);
})
}
);
}
您可以通过console.log(“collection find res:+JSON.stringify(res)),检查[object object]中的内容吗;当我这样做
TypeError:将循环结构转换为JSON
或请执行console.log(res)时,我会遇到这个异常,所以要确切地知道您得到了什么,它正在打印巨大的JSON。请检查我已经编辑了我的问题,您可以通过console.log(“collection find res:+JSON.stringify(res)),检查[object object]中的内容吗;当我这样做TypeError:将循环结构转换为JSON
或请执行console.log(res)时,我会遇到这个异常,所以要确切地知道您得到了什么,它正在打印巨大的JSON。请检查我是否编辑了我的问题实际上我的数据库中有三个不同的集合。文本回应就是其中之一。如何在不同集合中查询?模型名称是否应该与mongodb中的集合完全相同?我不确定,为了映射,我们需要使用相同的.db.collection(“代理”),这里代理不是集合名称,而是我的数据库名称?。如何在单个数据库中查询不同的集合?您做错了,请使用Model
而不是数据库。即使将db.collection更改为Model name也不适用于Measual。实际上,我的数据库中有三个不同的集合。文本回应就是其中之一。如何在不同集合中查询?模型名称是否应该与mongodb中的集合完全相同?我不确定,为了映射,我们需要使用相同的.db.collection(“代理”),这里代理不是集合名称,而是我的数据库名称?。如何在单个数据库中查询不同集合?您做错了,请使用Model
而不是数据库。即使将db.collection更改为Model name,对我也不起作用