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.
node.js mongodb
add a comment |
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.
node.js mongodb
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 likedocument.cookie = Animal.distinct("traits")
. Then I might retrieve it back usingvar 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
add a comment |
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.
node.js mongodb
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
node.js mongodb
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 likedocument.cookie = Animal.distinct("traits")
. Then I might retrieve it back usingvar 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
add a comment |
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 likedocument.cookie = Animal.distinct("traits")
. Then I might retrieve it back usingvar 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
add a comment |
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 :) ?
First, thanks for your prompt response. But it didn't work for me. Onconsole.log(req.session.animals);
, the log showsQuery { _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 triedvar traits = Animal.distinct.("traits"); console.log(traits)
before, it gave me the same result.
– Q Yang
Oct 13 '17 at 0:46
add a comment |
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);
});
});
But I want to use this variable out of callback function, not inside it.
– Q Yang
Oct 13 '17 at 0:31
add a comment |
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 :) ?
First, thanks for your prompt response. But it didn't work for me. Onconsole.log(req.session.animals);
, the log showsQuery { _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 triedvar traits = Animal.distinct.("traits"); console.log(traits)
before, it gave me the same result.
– Q Yang
Oct 13 '17 at 0:46
add a comment |
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 :) ?
First, thanks for your prompt response. But it didn't work for me. Onconsole.log(req.session.animals);
, the log showsQuery { _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 triedvar traits = Animal.distinct.("traits"); console.log(traits)
before, it gave me the same result.
– Q Yang
Oct 13 '17 at 0:46
add a comment |
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 :) ?
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 :) ?
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. Onconsole.log(req.session.animals);
, the log showsQuery { _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 triedvar traits = Animal.distinct.("traits"); console.log(traits)
before, it gave me the same result.
– Q Yang
Oct 13 '17 at 0:46
add a comment |
First, thanks for your prompt response. But it didn't work for me. Onconsole.log(req.session.animals);
, the log showsQuery { _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 triedvar 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
add a comment |
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);
});
});
But I want to use this variable out of callback function, not inside it.
– Q Yang
Oct 13 '17 at 0:31
add a comment |
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);
});
});
But I want to use this variable out of callback function, not inside it.
– Q Yang
Oct 13 '17 at 0:31
add a comment |
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);
});
});
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);
});
});
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 usingvar 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