0

for first block of code , console.log("ff : "+ff) prints exactly like this :

ff : { _id : sd845y3hishofiuwhei , fullname : 'sachin' }

console.log("ff JSON : "+JSON.stringify(ff)); // this line prints like below : 

ff JSON : [{"_id":"sd845y3hishofiuwhei" , "fullname" : "sachin" }]

how can i get that "sachin" separately in a separate variable ?

var fullname=SampleModel.find({uname:req.session.uname},{fullname : true},function(err,ff){
    if(!err){
        console.log("ff : "+ff);
        console.log("ff JSON  : "+JSON.stringify(ff));
        return ff;
    }else { 
        return null;
    }
});

the below block of code is giving error like "invalid object id " . what can be the problem ?

var ret= AnotherModel.find({_id : { $nin : ["1","2"] } }, function(err,details){
    if(!err) {
        res.send(details);
    }
    else {
        console.log("Error : "+err);
        res.send(err);
    }
});

i am implementing textarea like this

<textarea id="myarea" cols="100" rows="20"   ></textarea>

output box is changing its height when i change rows , but that cols , when i change "cols" , its width not changing . the output box also has small width , i check in both chrome and mozilla. Whats the solution ? Thanks

1
  • This has nothing to do with mongodb Commented Apr 16, 2012 at 13:31

1 Answer 1

1

I understand that you use mongoose and not only mongodb driver.

In the first block of code, the ff value isn't a string but an array of objects.

You can access "sachin" variable just doing something like : var fullname = ff[0].fullname; Of course, it's best to iterate through the array but you get the idea.

If you want only one object to be retrieved, you should use "findOne" function instead and then you would be able to access "sachin" like this : var fullname = ff.fullname;

For the second question, you should use Query system. Queries are delayed requests that you can chain and tune more finely. To delay your query, just ommit the callback function : var query = Model.find({});

Then, add the filter you want, in your case : query.where('_id').nin(['1', '2']);

Finally, execute it :

query.exec(function (err, docs) {
    // called when the `query.complete` or `query.error` are called
    // internally
});

Informations about finding docs and about queries.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks for the answer, can anyone tell me solution for the text area problem i have mention at the end ?
Model.findById("idstring") Model.find({_id : "idstring"}) Both of the above are giving error "INVALID OBJECT ID " , only for id i am getting error , for others like {uname : "username"} , i am getting solution . So, i think i am passing id in the wrong format , what is the correct format in which i can give _id as a parameter .
Have you declared _id as an Schema.ObjectId in your item Schema ? Please post your schema declaration code.
i didnt declare _id at all. When i insert any data, it creates new _id . Anyway , please tell me this one . I have a collection with unique entries username , _id where _id is automatically created by mongo. How should i refer this particular user in another collection with better performance ? i mean, whether referrng by username or _id or a normal "id" that we assign it some value ? what is the best way ?
my schema : var user = new Schema({ uname : { type : String , required:true ,unique:true}, pwd : {type : String , required:true}, }); now, i tried addng this field _id : {type : ObjectId} which resulted in an error saying "Reference Errror : ObjectId is not defined "
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.