1
{"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"[email protected]","last_login":"11:25:24 AM","name_surname":"user1","password":"qwerty123"}
{"__v":0,"_id":{"$oid":"55ef49dd5d610eab18719deb"},"admin":true,"email":"[email protected]","last_login":"12:25:24 AM","name_surname":"user2","password":"qwerty123"}
{"__v":0,"_id":{"$oid":"55f0173bb3322bf560724fd1"},"admin":true,"email":"[email protected]","last_login":"10:25:24 AM","name_surname":"user3","password":"qwerty123"}

Hello, I working in nodeJS file and there I have a collection of JSON objects and I would like to make a search through it. For each user from this list I need to compare the field "last_login" . I am new to nodeJS can someone help? Thank you!

This is what i have tried:

User.find({}, {last_login: }, function(err, docs) {
    if (err) {
      return res.json({ success: false, message: 'Failed to display last logins.' });
    }
    docs.forEach(function(doc, index) {
      res.json({success: true, message: 'time of last login', last_login: doc.last_login});
    })
    //res.json(users);
  });
});   

Where last_login is a field in the User object and basically I need to iterate over all users in the db and extract only the last_login and display in in the response.I don’t know what value to put in the find() inside the curly braces this is the part where I am stuck

I’ve slightly changed the function and it returns a JSON object containing the info about one user that is matched with the search query. The problem is, the console displays the result, as a whole object, although I want to get only a specific key value pair and namely last_login: value

function searchByUserName(name_surname) {
    return list.filter(function(user) {
        return user.name_surname === name_surname;
    });
}
var a = searchByUserName('user1');
for (last_login in a ) {
  if (a.hasOwnProperty(last_login)) {
    console.log("last_login" + "=" + JSON.stringify(a[last_login]))
  }
}

Can you tell me please, what change to make in order to get only the last_login key here is a sample result from the console.log() that I receive:

last_login={"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"[email protected]","last_login":"11:25:24 AM","name_surname":"user1","password":"qwerty123"}

although I want last_login = “last_login”: 11:25:24 AM

6
  • What have to tried and where you got stuck? Commented Sep 10, 2015 at 12:32
  • so what do you expect as result? Commented Sep 10, 2015 at 12:35
  • I have updated the post Commented Sep 10, 2015 at 12:36
  • have you got the answer ? Commented Sep 14, 2015 at 2:38
  • not yet ( @Mritunjay Commented Sep 14, 2015 at 7:03

2 Answers 2

1

Assuming it's an array of objects like bellow.

var users = [{"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"[email protected]","last_login":"11:25:24 AM","name_surname":"user1","password":"qwerty123"},
{"__v":0,"_id":{"$oid":"55ef49dd5d610eab18719deb"},"admin":true,"email":"[email protected]","last_login":"12:25:24 AM","name_surname":"user2","password":"qwerty123"},
{"__v":0,"_id":{"$oid":"55f0173bb3322bf560724fd1"},"admin":true,"email":"[email protected]","last_login":"10:25:24 AM","name_surname":"user3","password":"qwerty123"}];

you can create a function like bellow

function searchByLastLogin(last_login) {
    return users.filter(function(user) {
        return user.last_login === last_login;
    });
}

console.log(searchByLastLogin("12:25:24 AM"));
console.log(searchByLastLogin("10:25:24 AM"));
console.log(searchByLastLogin("11:25:24 AM"));

It will retrun a array of users whose last_login will match to given parameter last_login.

Update

What I understood from your comment bellow, you want last logins of every user.

For that you can do something like bellow

var last_logins = users.map(function(user){
    return user.last_login;
});

console.log(last_logins); //prints [ '11:25:24 AM', '12:25:24 AM', '10:25:24 AM' ]

References

filter | map

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

3 Comments

Thank you for your response! I have an additional, possibly silly question: I see you named the array of the users in the db “users”, but the problem is I don’t know how is the implicit name of the db array in the MongoDB. I think “User” is the name of the collection but I am not sure how to iterate through every item (JSON object) in the collection. Because what I want is to get a list of all the last logins (last_login) for every user.
@Chris I've just taken your data in a variable, I'm not getting what db you are reffering to.
I’ve slightly changed the function and it returns a JSON object containing the info about one user that is matched with the search query. The problem is, the console displays the result, as a whole object, although I want to get only a specific key value pair and namely last_login: value
0

I don’t know what value to put in the find() inside the curly braces this is the part where I am stuck

If I understand correctly, you only want to get the last_login field for the user model, and that's what you're struggling with ?

According to the documentation, this should work if you only want to get that field :

User.find({}, {last_login:1, _id:0})

1 Comment

Thank you, your answer works but it only displays the field for the first user in the db. However, I need to iterate through all the users in the db and for each to perform this task. Can you give me a proper way to iterate through all the db?

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.