Insert collection server side


#1

When I do db.persons.find(); I have one document { “_id” : “yn4toWcMXtsK7E9Ds”, “0” : { “_id” : “1010”, “name” : “xxx”, “address” : “some address” } }
This document added in my database in server side.

My code:

Meteor.startup(function () {
if (Persons.find().count() === 0) {
var person = [
{
’_id’: “1010”,
‘name’: “xxx”,
‘address’: “some address”
}
];
Persons.insert(person);
}
)};

Why the field “0” added ? How can I access it ? if not, how can I change the way it’s writen in the database ?
For example, when I do db.persons.find(); I want something like: { “_id” : “1010”, “name” : “xxx”, “address” : “some address” }

:slight_smile:


#2

var person = {
’_id’: “1010”,
‘name’: “xxx”,
‘address’: “some address”
};


#3

Either you use Persons.findOne() which gets only one document or you use Persons.find().fetch(), which will give you an array of objects in the collection.

Only using .find() will get the cursor, you need the actual data however.


#4

@mrw34 thank you :wink: