maicmi
August 13, 2017, 9:38pm
1
If I have some data in my collection, and I find specific data by Collections.find({ field1: "good"}).count;
If it’s match 3 from 10 and I would like to display the list number from find result.
How can I do this work?
Item user
1 abc
2 def
3 ghi
I have tried “length” but it shows the Item like count.
I’m afraid you have to do a const totalCount = Collections.find().count
to get the total count of all docs in a seperate call.
To minimize the amount of calls you make, if the total count doesn’t change that often, you could store that number somewhere for that session.
manuel
August 13, 2017, 11:10pm
3
Collections.find({ field1: "good"}).reduce((col, value, index) => {
col.push({
item: index + 1,
user: value.user
});
return col;
}, []);
maicmi
August 14, 2017, 1:05am
4
Hi manuel,
Thank you very much.
So if I would like to put your code in my project, how can I do that?
please see below my code.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'react-router-dom';
import { Table, Alert, Button } from 'react-bootstrap';
import { Meteor } from 'meteor/meteor';
import { createContainer } from 'meteor/react-meteor-data';
import { Registers } from '../../api/registers';
const handleRemove = (registerId) => {
if (confirm('Are you sure? This is permanent!')) {
Meteor.call('registers.remove', registerId, (error) => {
if (error) {
console.log(error.reason, 'danger');
} else {
console.log('success');
}
});
}
};
const Sodaban = ({ sodaban, completedCount, match, history }) => (
<div className="Sodaban">
<div className="page-header clearfix">
<h4 className="pull-left">Sodaban </h4> <h4>Join {completedCount}</h4>
</div>
{sodaban.length ? <Table responsive>
<thead>
<tr>
<th>Item</th>
<th>Salution</th>
<th>First Name</th>
<th>Last Name</th>
<th>Province</th>
<th>Bhumdharm Level</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{sodaban.map(({ _id, regsId, salution, firstname, lastname, province, bhumdharmlevel, status }) => (
<tr key={_id}>
<td>{regsId}</td>
<td>{salution}</td>
<td>{firstname}</td>
<td>{lastname}</td>
<td>{province}</td>
<td>{bhumdharmlevel}</td>
<td>{status}</td>
</tr>
))}
</tbody>
</Table> : <Alert bsStyle="warning">Nobody yet!</Alert>}
</div>
);
Sodaban.propTypes = {
sodaban: PropTypes.arrayOf(PropTypes.object).isRequired,
completedCount: PropTypes.number.isRequired,
match: PropTypes.object.isRequired,
history: PropTypes.object.isRequired,
};
export default createContainer(() => {
const subscription = Meteor.subscribe('registers');
return {
sodaban: Registers.find({ bhumdharmlevel: "sodaban", status: "come" }).fetch(),
completedCount: Registers.find({ bhumdharmlevel: "sodaban", status: "come" }).count(),
};
}, Sodaban);
manuel
August 14, 2017, 1:59am
5
Tldr, instead of using collection.find use collection.find.reduce
1 Like
maicmi
August 14, 2017, 3:52am
6
Thank you very much, manuel.