Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:47:17+00:00 2026-06-12T16:47:17+00:00

I have a basic document with a ‘checked_in’ flag in my express app: module.exports

  • 0

I have a basic document with a ‘checked_in’ flag in my express app:

module.exports = Book= mongoose.model('Book', new Schema({
 name : String,
 checked_in : Boolean
},{ collection : 'Book' }));

I wanted to keep a log of when books are checked in and out so I came up with another schema:

var action = new Schema({
 checked_in: Boolean,     
});

module.exports = Activity = mongoose.model('Activity', new Schema({
 book_id: String,
 actions: [action]
},{ collection : 'Activity' }));

The ‘book_id’ should be the document id of a book and when I update a book I need to either create or update the activity log for that book with a new item inside of actions:

exports.update = function(req, res){  
    return Book.findById(req.params.id, function(err, book) {  
       var activity = new Activity({book_id: book.id});
       activity.actions.push({
           checked_in: req.body.checked_in,
       });

       Activity.update({ book_id: book.id}, activity.toObject(), { upsert: true }));

       book.checked_in = req.body.checked_in;
       return device.save(function(err) {
           return res.send(book);
       });
    });
};

The problem I am having is that nothing gets inserted into the Activity collection. If I use .save() then i just get lots of duplicates in the collection.

UPDATE

I’ve started re-working things with the advice given below but am still not having any luck with this. Here’s what I have now:

module.exports = Activity = mongoose.model('Activity', new Schema({
  book_id: Schema.ObjectId,
  actions: [new Schema({
    checked_in: Boolean,
    last_user: String
  })]
},{ collection : 'Activity' }));

Here’s the update code now:

exports.update = function(req, res){  
  // TODO: Check for undefined.
  return book.findById(req.params.id, function(err, book) {    
    if(!err) {
      // Update the book.
      book.checked_in = req.body.checked_in;
      book.last_user = req.body.last_user;    
      book.save();

      // If there's no associated activity for the book, create one. 
      // Otherwise update and push new activity to the actions array.
      Activity.findById(book._id, function (err, activity) {
        activity.actions.push({
          checked_in: req.body.checked_in,
          last_user: req.body.last_user
        })

        activity.save();      
      });
    }
  });
};

What I want to end up with is a document for each book with an array of check outs/ins that gets updated each time someone checks a book in or out. i.e:

{
    book_id: "5058c5ddeeb0a3aa253cf9d4",
    actions: [
        { checked_in: true, last_user: 'ralph' },
        { checked_in: true, last_user: 'gonzo' },
        { checked_in: true, last_user: 'animal' }
    ]
}

Eventually I will have a time stamp within each entry.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:47:18+00:00Added an answer on June 12, 2026 at 4:47 pm

    There are a couple problems:

    1. You’re trying to find the book’s activity doc using findById using the book’s id instead of the activity’s id.
    2. You’re not handling the case where the book’s activity doc doesn’t exist yet.

    Try this instead:

    Activity.findOne({book_id: book._id}, function (err, activity) {
      if (!activity) {
        // No Activity doc for the book yet, create one.
        activity = new Activity({book_id: book._id});
      }
      activity.actions.push({
        checked_in: req.body.checked_in,
        last_user: req.body.last_user
      });
    
      activity.save();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to javascript and have a very basic question: I have this
I have a basic jQuery tabs system going: $(document).ready(function(){ $('#tabs div.jdiv').hide(); $('#tabs div.jdiv:first').fadeIn(slow); $('#tabs
I have a basic model which references both ForeignKeys and ManyToMany objects. In edit
I have a basic XML document setup like this: <rss> <channel> </channel> </rss> I
I have this following vary basic svg document with 1 flowtext and 1 text
I have parsed a basic xml document with the following format <?xml version=1.0?> <data>
I have this basic example: <!doctype HTML> <html> <head> <script src=jquery-1.4.2.min.js type=text/javascript></script> <script> $(document).ready(function(){
I have a basic collection that does a fetch on document ready: ie: message_list.fetch({
I have basic Spring MVC 3 setup for i18n where I can show labels
I have basic idea on Kilo Virtual Machine on Mobiles , I have clear

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.