How to assign a mongodb query result to a variable?











up vote
0
down vote

favorite












Now I connect Node.js with mongodb and query databse using express. Say there is schema called Animal, which has an array field called traits. When I query:



db.animals.distinct("traits")


I got the result as:



['playful', 'funny', 'lazy', 'loyal']


Is it possible that I pass this result to a variable for later use? Thanks a lot.










share|improve this question


















  • 1




    you could store them in a session, cookie or may be at Redis..and then retrieve when needed
    – Raja ji
    Oct 12 '17 at 17:31












  • You mean I can code like document.cookie = Animal.distinct("traits"). Then I might retrieve it back using var traits = document.cookie. Is it right?
    – Q Yang
    Oct 12 '17 at 17:39










  • Did the answer worked..???
    – Raja ji
    Oct 12 '17 at 19:05















up vote
0
down vote

favorite












Now I connect Node.js with mongodb and query databse using express. Say there is schema called Animal, which has an array field called traits. When I query:



db.animals.distinct("traits")


I got the result as:



['playful', 'funny', 'lazy', 'loyal']


Is it possible that I pass this result to a variable for later use? Thanks a lot.










share|improve this question


















  • 1




    you could store them in a session, cookie or may be at Redis..and then retrieve when needed
    – Raja ji
    Oct 12 '17 at 17:31












  • You mean I can code like document.cookie = Animal.distinct("traits"). Then I might retrieve it back using var traits = document.cookie. Is it right?
    – Q Yang
    Oct 12 '17 at 17:39










  • Did the answer worked..???
    – Raja ji
    Oct 12 '17 at 19:05













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Now I connect Node.js with mongodb and query databse using express. Say there is schema called Animal, which has an array field called traits. When I query:



db.animals.distinct("traits")


I got the result as:



['playful', 'funny', 'lazy', 'loyal']


Is it possible that I pass this result to a variable for later use? Thanks a lot.










share|improve this question













Now I connect Node.js with mongodb and query databse using express. Say there is schema called Animal, which has an array field called traits. When I query:



db.animals.distinct("traits")


I got the result as:



['playful', 'funny', 'lazy', 'loyal']


Is it possible that I pass this result to a variable for later use? Thanks a lot.







node.js mongodb






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 12 '17 at 17:29









Q Yang

11018




11018








  • 1




    you could store them in a session, cookie or may be at Redis..and then retrieve when needed
    – Raja ji
    Oct 12 '17 at 17:31












  • You mean I can code like document.cookie = Animal.distinct("traits"). Then I might retrieve it back using var traits = document.cookie. Is it right?
    – Q Yang
    Oct 12 '17 at 17:39










  • Did the answer worked..???
    – Raja ji
    Oct 12 '17 at 19:05














  • 1




    you could store them in a session, cookie or may be at Redis..and then retrieve when needed
    – Raja ji
    Oct 12 '17 at 17:31












  • You mean I can code like document.cookie = Animal.distinct("traits"). Then I might retrieve it back using var traits = document.cookie. Is it right?
    – Q Yang
    Oct 12 '17 at 17:39










  • Did the answer worked..???
    – Raja ji
    Oct 12 '17 at 19:05








1




1




you could store them in a session, cookie or may be at Redis..and then retrieve when needed
– Raja ji
Oct 12 '17 at 17:31






you could store them in a session, cookie or may be at Redis..and then retrieve when needed
– Raja ji
Oct 12 '17 at 17:31














You mean I can code like document.cookie = Animal.distinct("traits"). Then I might retrieve it back using var traits = document.cookie. Is it right?
– Q Yang
Oct 12 '17 at 17:39




You mean I can code like document.cookie = Animal.distinct("traits"). Then I might retrieve it back using var traits = document.cookie. Is it right?
– Q Yang
Oct 12 '17 at 17:39












Did the answer worked..???
– Raja ji
Oct 12 '17 at 19:05




Did the answer worked..???
– Raja ji
Oct 12 '17 at 19:05












2 Answers
2






active

oldest

votes

















up vote
0
down vote













first install this library by npm install client-sessions



call this in your main js page



  var session = require('client-sessions');


give definition



app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));


Store the results in session called animals



    app.post('/getAnimals', function(req, res) {    
// inside your DB query callback{}
req.session.animals= db.animals.distinct("traits")
})


Retrieving data from session when required



console.log(req.session.animals);   //will give
['playful', 'funny', 'lazy', 'loyal']

console.log(req.session.animals[0]); // will give first element
playful


Let me know if this solution solved your issue :) ?






share|improve this answer























  • First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
    – Q Yang
    Oct 13 '17 at 0:46


















up vote
0
down vote













Yes. you can assign query result to variable. like this:



var a;
db.collection('animals', function(err, collection) {
collection.distinct("traits", function(err, results) {
a = results;
console.log(results);
});
});





share|improve this answer























  • But I want to use this variable out of callback function, not inside it.
    – Q Yang
    Oct 13 '17 at 0:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46715597%2fhow-to-assign-a-mongodb-query-result-to-a-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













first install this library by npm install client-sessions



call this in your main js page



  var session = require('client-sessions');


give definition



app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));


Store the results in session called animals



    app.post('/getAnimals', function(req, res) {    
// inside your DB query callback{}
req.session.animals= db.animals.distinct("traits")
})


Retrieving data from session when required



console.log(req.session.animals);   //will give
['playful', 'funny', 'lazy', 'loyal']

console.log(req.session.animals[0]); // will give first element
playful


Let me know if this solution solved your issue :) ?






share|improve this answer























  • First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
    – Q Yang
    Oct 13 '17 at 0:46















up vote
0
down vote













first install this library by npm install client-sessions



call this in your main js page



  var session = require('client-sessions');


give definition



app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));


Store the results in session called animals



    app.post('/getAnimals', function(req, res) {    
// inside your DB query callback{}
req.session.animals= db.animals.distinct("traits")
})


Retrieving data from session when required



console.log(req.session.animals);   //will give
['playful', 'funny', 'lazy', 'loyal']

console.log(req.session.animals[0]); // will give first element
playful


Let me know if this solution solved your issue :) ?






share|improve this answer























  • First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
    – Q Yang
    Oct 13 '17 at 0:46













up vote
0
down vote










up vote
0
down vote









first install this library by npm install client-sessions



call this in your main js page



  var session = require('client-sessions');


give definition



app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));


Store the results in session called animals



    app.post('/getAnimals', function(req, res) {    
// inside your DB query callback{}
req.session.animals= db.animals.distinct("traits")
})


Retrieving data from session when required



console.log(req.session.animals);   //will give
['playful', 'funny', 'lazy', 'loyal']

console.log(req.session.animals[0]); // will give first element
playful


Let me know if this solution solved your issue :) ?






share|improve this answer














first install this library by npm install client-sessions



call this in your main js page



  var session = require('client-sessions');


give definition



app.use(session({
cookieName: 'session',
secret: 'random_string_goes_here',
duration: 30 * 60 * 1000,
activeDuration: 5 * 60 * 1000,
}));


Store the results in session called animals



    app.post('/getAnimals', function(req, res) {    
// inside your DB query callback{}
req.session.animals= db.animals.distinct("traits")
})


Retrieving data from session when required



console.log(req.session.animals);   //will give
['playful', 'funny', 'lazy', 'loyal']

console.log(req.session.animals[0]); // will give first element
playful


Let me know if this solution solved your issue :) ?







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 12 '17 at 17:54

























answered Oct 12 '17 at 17:47









Raja ji

1,1911619




1,1911619












  • First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
    – Q Yang
    Oct 13 '17 at 0:46


















  • First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
    – Q Yang
    Oct 13 '17 at 0:46
















First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
– Q Yang
Oct 13 '17 at 0:46




First, thanks for your prompt response. But it didn't work for me. On console.log(req.session.animals);, the log shows Query { _mongooseOptions: {}, mongooseCollection: NativeCollection { collection: Collection { s: [Object] }, opts: { bufferCommands: true, capped: false }, name: 'animals',..... How do I know the client session start correctly? Because I tried var traits = Animal.distinct.("traits"); console.log(traits)before, it gave me the same result.
– Q Yang
Oct 13 '17 at 0:46












up vote
0
down vote













Yes. you can assign query result to variable. like this:



var a;
db.collection('animals', function(err, collection) {
collection.distinct("traits", function(err, results) {
a = results;
console.log(results);
});
});





share|improve this answer























  • But I want to use this variable out of callback function, not inside it.
    – Q Yang
    Oct 13 '17 at 0:31















up vote
0
down vote













Yes. you can assign query result to variable. like this:



var a;
db.collection('animals', function(err, collection) {
collection.distinct("traits", function(err, results) {
a = results;
console.log(results);
});
});





share|improve this answer























  • But I want to use this variable out of callback function, not inside it.
    – Q Yang
    Oct 13 '17 at 0:31













up vote
0
down vote










up vote
0
down vote









Yes. you can assign query result to variable. like this:



var a;
db.collection('animals', function(err, collection) {
collection.distinct("traits", function(err, results) {
a = results;
console.log(results);
});
});





share|improve this answer














Yes. you can assign query result to variable. like this:



var a;
db.collection('animals', function(err, collection) {
collection.distinct("traits", function(err, results) {
a = results;
console.log(results);
});
});






share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 12 '17 at 17:57

























answered Oct 12 '17 at 17:43









kgangadhar

1,56521124




1,56521124












  • But I want to use this variable out of callback function, not inside it.
    – Q Yang
    Oct 13 '17 at 0:31


















  • But I want to use this variable out of callback function, not inside it.
    – Q Yang
    Oct 13 '17 at 0:31
















But I want to use this variable out of callback function, not inside it.
– Q Yang
Oct 13 '17 at 0:31




But I want to use this variable out of callback function, not inside it.
– Q Yang
Oct 13 '17 at 0:31


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46715597%2fhow-to-assign-a-mongodb-query-result-to-a-variable%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python