简体   繁体   中英

Meteor - Selecting first two objects in a mongodb collection

I have a signup list I am trying to build out, so people can signup, be rearranged, and deleted. Here is the problem I am having; I want to select the first two items (keys) from a MongoDb collection. How can I do this so the next two people in line are displayed in a new template. I know that find might be able to help, but I'm really unsure of what exactly I am searching for. is it easier to limit everything in the #each loop?

This is my code:

Server Side:

Meteor.publish( 'artists', function() {
  return Artists.find( {}, { sort: { order: 1 } } );
});

Client Side:

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';
import { Session } from 'meteor/session'


import './main.html';

var $ = require('jquery');
require('fancybox')($);

let initSortable = ( sortableClass ) => {
  let sortableList = $( sortableClass );
  sortableList.sortable( 'destroy' );
  sortableList.sortable();
  sortableList.sortable().off( 'sortupdate' );
  sortableList.sortable().on( 'sortupdate', () => {
    updateIndexes( '.sortable' );
  });
};

let updateIndexes = ( sortableClass ) => {
  let items = [];

  $( `${sortableClass} li` ).each( ( index, element ) => {
    items.push( { _id: $( element ).data( 'id' ), order: index + 1 } );
  });

  Meteor.call( 'updateArtistOrder', items, ( error ) => {
    if ( error ) {
      console.log( error.reason );
    }
  });
};

Template.artists.onCreated( () => {
  let template = Template.instance();

  template.subscribe( 'artists', () => {
    initSortable( '.sortable' );
  });

});

Template.artists.events({
  'submit #add-artist' ( event, template ) {
    event.preventDefault();

    let artist = {
      artistName: template.find( '[name="artistName"]' ).value
    };

    Meteor.call( 'addArtist', artist, ( error ) => {
      if ( error ) {
        console.log( error );
      } else {
        $( event.target ).get(0).reset();
        $( '[name="artistName"]' ).focus();
        initSortable( '.sortable' );
      }
    });
  },

    'click .delete-artist' ( event, template ) {
    if ( confirm( 'Are you sure? This is permanent!' ) ) {
      Meteor.call( 'deleteArtist', this._id, ( error ) => {
        if ( error ) {
          console.log( error );
        } else {
          updateIndexes( '.sortable' );
        }
      });
    }
  }

});

Template.artists.helpers({
  artists() {
    return Artists.find();
  }
});

Collection:

Artists = new Mongo.Collection( 'artists' );

Artists.allow({
  insert() { return false; },
  update() { return false; },
  remove() { return false; }
});

Artists.deny({
  insert() { return true; },
  update() { return true; },
  remove() { return true; }
});

Template:

   <template name="artists">
  <div class="container">
    <h3 class="page-header">Next Up</h3>

    <ul class="list-group sortable">
      {{#each artists}}
        <li data-id="{{_id}}" class="list-group-item clearfix">
          <span class="pull-left">{{order}}. {{artistName}}</span>
          <i class="fa fa-remove pull-right text-danger delete-artist"></i>
        </li>
      {{/each}}
    </ul>

    <form id="add-artist">
      <div class="row">

        <div class="col-xs-12 col-sm-5">
          <label for="artistName">Register Here:</label>
          <input type="text" name="artistName" class="form-control" placeholder="Name">
        </div>
              </div>
        <div class="row">
          <div class="col-xs-12 col-sm-2">
            <button type="submit" class="btn btn-success btn-block">Add Name</button>
          </div>
        </div>

    </form>
  </div>
</template>

There are two ways to implement this:

  1. Sort the data from the publication in the client side and maintain a marker for the postion. Let says you have 10 documents, you can sort them out in an order and set the marker as 2(you can use session variable for this) and similarly for the next template as 4 and so on.

  2. Use this package . This package allows to you subscribe the publication with pagination. here you can limit the documents to 2 and accordingly use the functions mentioned in the docs to achieve this.

Try to do it in your client-side helper :

Template.artists.helpers({
  artists() {
    return Artists.find({}, { limit: 2, sort: {order: 1} });
  }
});

With that, only two objects will be retrieved from your mongodb collection.

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