mongodb ObjectId find with sails, returns 所有数据
mongodb ObjectId find with sails, returns all the data
在我的 sails 控制器中,我实现了一个查找用户的功能。但是当我尝试使用 ObjectId 进行搜索时,它 returns 所有数据。我找不到任何理由。但是当我创建一个新的单独字段 "userID" 并使用 "userID" 查找时它会起作用。我认为问题是使用 "ObjectId" 可能是什么问题。
module.exports = {
updateOrders : function(req,res){
var ObjectId = require('mongodb').ObjectID;
console.log("req " + req);
var data = req.body;
var obj = [];
console.log("\ndata "+(data));
var jdata = JSON.stringify(data);
console.log("\njdata: "+jdata);
data.forEach(function(orders){
var objid = ObjectId(orders.id);
console.log("Obj orders id :" + objid);
ApplicationOrder.find({id: ObjectId(orders.id)}).exec(function (err, order) {
if (err){
return done(err);
}
console.log('order ' + order);
obj.push(order);
});
})
res.send(obj);
}
};
Json数据:打印在控制台。
{
"_id" : ObjectId("59a8f820caf1cae37af72c0c"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b70",
"name" : "Short x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "short_x.png"
}
],
"totalQty" : 218
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan@yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:12.584Z"),
"updatedAt" : ISODate("2017-09-01T06:03:12.584Z")
}
{
"_id" : ObjectId("59a8f82fcaf1cae37af72c0d"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b6d",
"name" : "Shirt x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "shirt_x.png"
}
],
"totalQty" : 244
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan@yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:27.022Z"),
"updatedAt" : ISODate("2017-09-01T06:03:27.022Z")
}
{
"_id" : ObjectId("59a8f83ecaf1cae37af72c0e"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b71",
"name" : "Beach Suit",
"qty" : 2,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "cloth3.png"
}
],
"totalQty" : 238
}
],
"amount" : "500",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan@yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:42.439Z"),
"updatedAt" : ISODate("2017-09-01T06:03:42.439Z")
}
对于标准帆。js/waterline 查询,您必须在查询参数中使用 id 作为字符串,不要使用 ObjectId(ordersId)
。
当您使用 sails 本机查询时,请使用 ObjectId(ordersId)
。
如果您正在使用任何 mongodb 适配器,则不要尝试通过 ObjectId(orders.id) 转换 ID。请尝试像
这样的正常查询
ApplicationOrder.find({'id':orders.id})
对于原生sails查询,您必须将id转换为objectId。
我终于能够解决这个问题。我不得不在 sails 中编辑我的模型 class 并且只使用 orders.id 模型 class,
_id:{type : 'objectid'}
当 search
.find({_id: orders.id})
这对我来说非常有用。
在我的 sails 控制器中,我实现了一个查找用户的功能。但是当我尝试使用 ObjectId 进行搜索时,它 returns 所有数据。我找不到任何理由。但是当我创建一个新的单独字段 "userID" 并使用 "userID" 查找时它会起作用。我认为问题是使用 "ObjectId" 可能是什么问题。
module.exports = {
updateOrders : function(req,res){
var ObjectId = require('mongodb').ObjectID;
console.log("req " + req);
var data = req.body;
var obj = [];
console.log("\ndata "+(data));
var jdata = JSON.stringify(data);
console.log("\njdata: "+jdata);
data.forEach(function(orders){
var objid = ObjectId(orders.id);
console.log("Obj orders id :" + objid);
ApplicationOrder.find({id: ObjectId(orders.id)}).exec(function (err, order) {
if (err){
return done(err);
}
console.log('order ' + order);
obj.push(order);
});
})
res.send(obj);
}
};
Json数据:打印在控制台。
{
"_id" : ObjectId("59a8f820caf1cae37af72c0c"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b70",
"name" : "Short x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "short_x.png"
}
],
"totalQty" : 218
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan@yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:12.584Z"),
"updatedAt" : ISODate("2017-09-01T06:03:12.584Z")
}
{
"_id" : ObjectId("59a8f82fcaf1cae37af72c0d"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b6d",
"name" : "Shirt x",
"qty" : 1,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "shirt_x.png"
}
],
"totalQty" : 244
}
],
"amount" : "250",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan@yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:27.022Z"),
"updatedAt" : ISODate("2017-09-01T06:03:27.022Z")
}
{
"_id" : ObjectId("59a8f83ecaf1cae37af72c0e"),
"appId" : "59a669431954a69e37c20b69",
"registeredUser" : "59a677811954a69e37c20b73",
"item" : [
{
"id" : "59a669441954a69e37c20b71",
"name" : "Beach Suit",
"qty" : 2,
"sku" : "1111",
"totWeight" : null,
"price" : "250",
"total" : "250",
"imgURL" : [
{
"img" : "cloth3.png"
}
],
"totalQty" : 238
}
],
"amount" : "500",
"customerName" : "dilakshan",
"deliverName" : "dilakshan",
"deliveryNo" : "11010",
"deliveryStreet" : "delgoda",
"deliveryCity" : "kandana",
"deliveryCountry" : "Sri Lanka",
"deliveryZip" : "11010",
"telNumber" : 94779967409,
"tax" : "0",
"shippingCost" : "0",
"shippingOpt" : "Flat Rate",
"email" : "s.dilakshan@yahoo.com",
"currency" : "$",
"paymentStatus" : "Pending",
"fulfillmentStatus" : "Pending",
"createdAt" : ISODate("2017-09-01T06:03:42.439Z"),
"updatedAt" : ISODate("2017-09-01T06:03:42.439Z")
}
对于标准帆。js/waterline 查询,您必须在查询参数中使用 id 作为字符串,不要使用 ObjectId(ordersId)
。
当您使用 sails 本机查询时,请使用 ObjectId(ordersId)
。
如果您正在使用任何 mongodb 适配器,则不要尝试通过 ObjectId(orders.id) 转换 ID。请尝试像
这样的正常查询 ApplicationOrder.find({'id':orders.id})
对于原生sails查询,您必须将id转换为objectId。
我终于能够解决这个问题。我不得不在 sails 中编辑我的模型 class 并且只使用 orders.id 模型 class,
_id:{type : 'objectid'}
当 search
.find({_id: orders.id})
这对我来说非常有用。