Need help in retrieving records Through Stored Javascript

396 views
Skip to first unread message

Maddy

unread,
Jan 3, 2011, 1:13:11 AM1/3/11
to mongodb-user
Hi Team,
Please tell me what is wrong with this Stored Javascript
function (ProductID) {
// var x = db.bank_product;
var ret = db.bank_product.find({ProductID:ProductID});
return ret;
}

Showing error like "value" : "DBQuery: testDB.bank_product ->
undefined" }

but whereas if i change the above stored javascript following as

function (ProductID) {
// var x = db.bank_product;
var ret = db.bank_product.findOne({ProductID:ProductID});
return ret;
}
ite working pretty fine. is that means the find wont work in stored
Javascript.

Eliot Horowitz

unread,
Jan 3, 2011, 1:15:08 AM1/3/11
to mongod...@googlegroups.com
Find returns a cursor.
You can't return a cursor from javascript.
You can call toArray() on it to return an array.

What are you trying to do with js?
In your case you clearly don't need it at all.
Are you sure you need JS?

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Maddy

unread,
Jan 3, 2011, 1:37:53 AM1/3/11
to mongodb-user
Thanks for your reply.
my scenario is like
i ll be getting few parameter based on that i need to return all
records in that parameter
eg:
if parameter is something like Audio devices i need to return list of
documents which is related to audio devices
and if the parameter is Apple(iPod) i need to display all products
documents related to that. is that i want.

and as you mentioned is it possible for me to convert those product
documents as array or
CAN YOU SUGGEST ME IS THERE ANY BETTER SOLUTION TO ATTAIN THIS.

Once again thanks for your reply

Thanks & Regards

Eliot Horowitz

unread,
Jan 3, 2011, 1:43:09 AM1/3/11
to mongod...@googlegroups.com
You can just use the normal query language.
What language are you using?
All languages have a find() method that takes a query that you can use
to do the filtering you mention.

Maddy

unread,
Jan 3, 2011, 2:24:14 AM1/3/11
to mongodb-user
Thanks a lot Eliot. .toArray is working for me i am using c# driver
API

Nat

unread,
Jan 3, 2011, 2:26:38 AM1/3/11
to mongodb-user
As Eliot said, the use case you want to do should be achieved without
using map-reduce. Any particular reason why would you need to use map-
reduce?

Maddy

unread,
Jan 4, 2011, 7:48:26 AM1/4/11
to mongodb-user
Hi,

Sorry for the late reply i was trying to achieve like this.

function (CategoryID,BrandID,AttributeID) {
var _returnVal = new Array ();
var _productIDs = new Array ();
if (CategoryID != '')
{
var src = CategoryID.toString();
var ret = db.bank_product.find({CategoryID:src},{ProductID:true});
_returnVal = _returnVal.concat(ret.toArray());
}
if (BrandID != '')
{
var src = BrandID.toString();
var ret = db.bank_product.find({BankBrandId:src},{ProductID:true});
_returnVal = _returnVal.concat(ret.toArray());
}
if (AttributeID != '')
{
var src = AttributeID.toString();
var ret = db.bank_product.find({"attributes.Attributeid":src},
{ProductID:true});
_returnVal = _returnVal.concat(ret.toArray());
}
_productIDs = _returnVal;
return _productIDs.length;

}

working fine when i changed as suggested in this thread.
But there is issue to get a distinct records. is that any way i can
achieve this to get distinct productID's

thanks & regards

Nat

unread,
Jan 4, 2011, 8:00:45 AM1/4/11
to mongodb-user
As mentioned earlier, what you are trying to do doesn't need map-
reduce. You can implement the same logic in C#. For distinct, it's
already supported by Mongo( http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct
). If you want to do it in C#, please consult C# driver documentation

Maddy

unread,
Jan 4, 2011, 8:21:14 AM1/4/11
to mongodb-user
Hi,
@Nat: what you told is absolutely right. what i am doing is like i am
developing a benchmark application for testing purpose on various
conditions to check the performance of my application so i need this
to be done in both the ways as in MAP-Reduce and also in c# with the
TIME Span of how much time its taking.

On Jan 4, 6:00 pm, Nat <nat.lu...@gmail.com> wrote:
> As mentioned earlier, what you are trying to do doesn't need map-
> reduce. You can implement the same logic in C#. For distinct, it's
> already supported by Mongo(http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct

Sam Millman

unread,
Jan 4, 2011, 8:35:52 AM1/4/11
to mongod...@googlegroups.com
Well I suppose this might produce a benchmark, though since a distinct operation for mogno already exists it would be like avoiding the DISTINCT operator in MySQL but getting all DISTINCT rows and benchmarking that..

The only way to get distinct rows that way is to store previous unique results and say:

if(!id in _seen_productIDs)

with ID being the ID you grab from _productIDs array.
Bow Chicka Bow Wow

Maddy

unread,
Jan 5, 2011, 7:21:10 AM1/5/11
to mongodb-user
Hi every one,

Again i have a problem in Stored Javascript.
that is., is it possible for me to get IN query (sub query in sql) for
MonogDB. please check what i did in the script

i used an array to store all values based on condition were i am doing
IN Query over the Array. what was wrong in that script please correct
me and also i mention the ERROR(Unsupported projection option: $in)
below

function (CategoryID,BrandID,AttributeID) {
var _returnVal = new Array ();
var _productIDs = new Array ();
var _temp='';
if (CategoryID != '')
{
var src = CategoryID.toString();
var ret = db.bank_product.find({CategoryID:src},{ProductID:{ $in :
_productIDs}},{ProductID:true});
_productIDs=ret.toArray();
}
if (BrandID != '')
{
var src = BrandID.toString();
var ret = db.bank_product.find({BankBrandId:src},{ProductID:{ $in :
_productIDs}},{ProductID:true});
_productIDs=ret.toArray();
}
if (AttributeID != '')
{
var src = AttributeID.toString();
var ret = db.bank_product.find({"attributes.Attributeid":src},
{ProductID:{ $in :_productIDs}},{ProductID:true});
_productIDs=ret.toArray();
}
return _productIDs.length;
}

Error:Error in executing Stored JavaScript
invoke failed: JS Error: uncaught exception: error: { "$err" :
"Unsupported projection option: $in", "code" : 13097 }
Type: MongoDB.MongoCommandException

nat....@gmail.com

unread,
Jan 5, 2011, 7:31:04 AM1/5/11
to mongod...@googlegroups.com
This line is incorrect.
var ret = db.bank_product.find({"attributes.Attributeid":src},
{ProductID:{ $in :_productIDs}},{ProductID:true});

It should be
var ret = db.bank_product.find({{"attributes.Attributeid":src},
{ProductID:{ $in :_productIDs}}},{ProductID:true});
Reply all
Reply to author
Forward
0 new messages