[英]mongo query from loopback issue
I am trying to query in mongo db from loopback model. 我正在尝试从回送模型中查询mongo db。 But i am not getting any result from monogdb 但是我没有从monogdb得到任何结果
This is how my document looks like in mongodb 这就是我的文档在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_"]
}
here is my datasource file 这是我的数据源文件
{
"db": {
"name": "db",
"connector": "memory"
},
"modroid-server": {
"host": "localhost",
"port": 27017,
"url": "",
"database": "modroid-server",
"password": "",
"name": "modroid-server",
"user": "",
"connector": "mongodb"
}
}
and here is model-config file 这是模型配置文件
{
"_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
}
}
and here is my code to query in mongodb 这是我在mongodb中查询的代码
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);
})
})
}
);
}
and when i hit that rest api. 而当我点击其余的api时。 I get this output 我得到这个输出
collection find res:[object Object]
collection find err:null
documnet result:
document err:null
please help me where i am doing thing wrong. 请在我做错事的地方帮助我。 I am stuck in that from couples of days. 几天以来,我一直陷入困境。
Edited 编辑
when i just print res it gives me huge data which starts as 当我只打印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 } },
Look like text-responses
is Not a model. 看起来text-responses
不是模型。
Try with Direct Model Agent. 尝试使用直接模型代理。
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);
})
});
}
Or By collection 或按收藏
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);
})
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.