简体   繁体   中英

Node.js Mongo Using $position While $push-ing Doesn't Work As Expected

I'm not that experienced with Node.js or Mongo, so please excuse any errors I may have in posting this.

I have a schema that looks like this:

var MessagesSchema = new mongoose.Schema({
    title: {
        type: String,
        default: "Untitled Message Group"
    },
    members: {
        type: Array
    },
    unreadCounts: {
        type: Array
    },
    admins: {
        type: Array
    },
    messages: {
        sender: {
            type: String
        },
        content: {
            type: String
        },
        date: {
            type: Number
        }
    }
});

New messages from users get sent into the "messages" section and their information gets put into the "sender", "content", and "date" arrays. I'm trying to make it so it loads only a certain number of messages at a time, and you can click a "Load More" button to load more messages. This is working, however my array is backwards and old messages get loaded first and the newest are loaded last. So, I am trying to make it so I reverse the array as data is input. Here is the old method I had for $push-ing:

Messages.updateOne({
    '_id': ObjectId(data[2])
}, {
    $push: {
        messages: {
            sender: data[1],
            content: data[0],
            date: Date.now()
        }
    },
    $inc: {
        "unreadCounts.$[]": 1
    }
}, function (err, result) {
   //...
});

Here's the new way that I tried:

Messages.updateOne({
    '_id': ObjectId(data[2])
}, {
    $push: {
        messages: {
            $each: [{sender: data[1], content: data[0], date: Date.now()}],
            $position: 0
        }
    },
    $inc: {
        "unreadCounts.$[]": 1
    }
}, function (err, result) {
    //...
});

And here's an example of some data that would be in it:

{
    "_id": ObjectId("5ce4990a11fa8f0d6e27344a"),
    //...
    //Other stuff
    //...
    "messages": [{
        "sender": "5c4ba709701212087e4d1bc9",
        "content": "1st message sent",
        "date": 1558485260497
    }, {
        "sender": "5c4ba709701212087e4d1bc9",
        "content": "2nd message sent",
        "date": 1558485261095
    }, {
        "sender": "5c4ba709701212087e4d1bc9",
        "content": "3rd message sent",
        "date": 1558485261446
    }]
}

The 3rd message is the last message that was sent. I would like the order to be reverse: The 3rd message would be at the top, then the 2nd message, then the 1st.

However, for some reason, using $position doesn't seem to work. Items get pushed onto the end of the array instead of being put in the beginning as expected.

Does anyone know if they could help me figure out why? Perhaps I just made a silly mistake? Thanks in advance!

Hope this helps : $push

Model :

var MessagesSchema = new mongoose.Schema({
   title: {
     type: String,
     default: "Untitled Message Group"
   },
   members: {
     type: Array
   },
   unreadCounts: {
      type: Array
   },
   admins: {
     type: Array
   },
   messages: [{
      sender: {
        type: String
      },
      content: {
        type: String
      },
      date: {
        type: Number
      }
   }]
 });

Existing Data :

{
  "_id" : ObjectId("5ce4990a11fa8f0d6e27344a"),
  "messages" : [
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "1st message sent",
        "date" : 1558485260497
    },
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "2nd message sent",
        "date" : 1558485261095
    },
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "3rd message sent",
        "date" : 1558485261446
    }
   ]
 }

Query :

db.messages.updateOne({
   "_id": ObjectId("5ce4990a11fa8f0d6e27344a")
},{
   $push: {
        messages: {
            $each: [{sender:"5c4ba709701212087e4d1bc9" , content: "4th message sent", date: Date.now()}],
            $sort: {date: -1}
        }
    }
 })

After Query Result :

{
  "_id" : ObjectId("5ce4990a11fa8f0d6e27344a"),
  "messages" : [
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "4th message sent",
        "date" : 1558575451749
    },
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "3rd message sent",
        "date" : 1558485261446
    },
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "2nd message sent",
        "date" : 1558485261095
    },
    {
        "sender" : "5c4ba709701212087e4d1bc9",
        "content" : "1st message sent",
        "date" : 1558485260497
    }
 ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM