For example, I have
[
{ name: "John" },
{ name: "Mike" },
{ name: "Homer" },
{ name: "Bart" },
{ name: "Dmitry" },
{ name: "Dan" }
]
How will I choose many objects with mongoose, if I use .limit(2) I will get [{ name: "John" }, { name: "Mike" }], I need to choose [{ name: "Bart" }, { name: "Dmitry" }]. In default JS this method looks like .slice(3,5). How will I do it with mongoose?
You can try something like this:
Model.find({name: {$in: ["Bart", "Dmitry"]}});
You can achieve this in mongodb by using skip(3).limit(2).
In Mongoose you can achieve this with myModel.find({}, 'name', {skip: 3, limit: 2}), you just have to insert values of skip and limit you want in third parameter of find.
Here's documentation with an example of skip and here's a link to a more popular answer of similar problem.
Edit: Note that this is a short-sighted solution, you should use something else for large or changing database.
Related
I am currently having problems using my data in react-d3-tree. The data coming from the server is not compatible with the format that react-d3-tree accepts.
I was told that doing the process using a foreach function for every item will work, but I think that will be really slow, especially whe the data is too big.
The data (as shown in my console) right now is like this:
0: {
series: 'Fate'
type: 'Servant'
class: 'Saber',
},
1: {
series: 'Fate',
type: 'Servant'
class: 'Archer',
},
2: {
series: 'Fate',
type: 'Demi-servant'
class: 'Shielder',
}
And I want to achieve this structure:
[
{
name: 'Fate',
children: [
{
name: 'Servant',
children: [
{
name: 'Saber',
children: []
},
{
name: 'Archer',
children: []
}
]
},
{
name: 'Demi-servant',
children: [
{
name: 'Shielder',
children: []
}
]
}
]
}
]
This is only sample data, and later on, and I might be converting data with more children. Is there any npm package that can be helpful?
Nevermind, I actually found my answer.
I'll share these useful links in case anyone finds this question too.
https://www.npmjs.com/package/shape-json
https://www.npmjs.com/package/shape-array
I have a many to many association like this:
Squad.belongsToMany(Member, {through: Membership});
Member.belongsToMany(Squad, {through: Membership});
How do I find all the Squads, and for each squad, show me the squad name and an array with the Members that each squad has associated with it?
UPDATED WITH WHAT I'VE BEEN TRYING AND WHAT RESULTS I GET:
I've been trying things like this:
Squad.findAll({
include: [{
model: Member,
required: true
}]
}).then(squads => {
// The rest of your logics here...
});
Here's a sample of what I get:
{ id: 3,
name: 'Knicks',
city: 'NYC',
state: 'NY',
'members.id': 3,
'members.name': 'Carmelo',
'members.city': 'NYC'
},
{ id: 3,
name: 'Knicks',
city: 'NYC',
state: 'NY',
'members.id': 2,
'members.name': 'Penny',
'members.city': 'Orlando',
'members.state': 'Florida'
}
But what I want is not multiples of the same object and individual member info. I'm trying to get something more like this:
{
id: 2,
name: 'Lakers',
members: [ memberInfo, memberInfo, memberInfo]
}
Is there a way to do that?
Assuming that you modeled your relations correctly, then you should be able to do something like
Squad.findAll({
include: [{
model: Member,
required: true
}]
}).then(squads => {
// The rest of your logics here...
});
Say I have an object that looks like this:
{
_id: ObjectID(1234),
name: 'Corvid',
friends: [{
name: 'Magpie',
friends: [{
name: 'Raven'
}, {
name: 'Jackdaw',
friends: [{
name: Friends.ALL_FRIENDS
}]
}]
}, {
name: 'Jay'
}]
}
Essentially, what I am trying to do is, for every entry in list of friends, replace the current object with the result of Friends.findOne({ name: obj.name }).
I am easy able to iterate through the structure like so.
function populate(obj=friends) {
if (obj.friends) {
return populate(obj.friends);
}
return friends.findOne({ name: obj.name });
}
However, what I am having trouble with is replacing that object at that specific area. How would one go about doing this?
I would like to know how to group documents according to a condition, and publish to the client.
Suppose I have the following documents:
[
{ name: 'John', createdAt: some_date_value_1 },
{ name: 'Jane', createdAt: some_date_value_2 },
{ name: 'Bob', createdAt: some_date_value_1 },
{ name: 'Jenny', createdAt: some_date_value_2 }
]
What can I do to publish a result like this?
{
some_date_value_1: [
{ name: 'John', createdAt: some_date_value_1 },
{ name: 'Bob', createdAt: some_date_value_1 }
],
some_date_value_2: [
{ name: 'Jane', createdAt: some_date_value_2 },
{ name: 'Jenny', createdAt: some_date_value_2 }
]
}
Any suggestions?
It depends of you want to do it on client or server.
First, the obvious solution: if you have no specific reason to store them with the first structure, store them directly using the second.
Second, another easy solution is to make in client. Here is a non tested code using undercore (bundled with meteor):
var yourCollection = CollectionName.find();
var yourCollectionByDate = _.groupBy(yourCollection , 'createdAt');
Third, you could still do it on server but either you will loose the reactivity of your collection (using for instance Collection.aggregate with a package) or have to transform and observe all changes afterwards (it would be a little overkill. However have a look here if you want more info)
A quick side note too: unless you want the users names to be unique, you should rely on mongo unique id (_.id) rather than on a name you set. That way, you are sure that you link to the right item (no duplicate).
I want to update an element of an array inside mongodb's document (I am using mongoose). Schema is something like:
{
..
arr : [{
foo: Number,
bar: [String],
name: String
}]
..
}
And my query is:
SomeModel.update({
_id: "id of the document",
arr: {
$elemMatch: {
_id: "_id assigned by mongoose to array element"
}
}
}, {
'arr.$': {
name: 'new name'
}
}).exec()
It just replaces whole array element say:
{
_id: "some objectId",
name: 'old name',
foo: 0,
}
to:
{
name: 'new name'
}
what I want:
{
_id: "some objectId",
name: 'new name',
foo: 0,
}
What I am curious to know if it is possible to achieve this in single update query ? (May be there is a silly mistake in my query :P or another approach)
I would also like to do update query like so:
{
$inc: { foo: 1},
$push: { bar: "abc"}
}
If you are still struggling with the whole implementation the full application of your statement is as follows:
SomeModel.update(
{
"arr._id": "123"
},
{
"$set": { "arr.$.name": "new name" },
"$inc": { "arr.$.foo": 1},
"$push": { "arr.$.bar": "abc" }
}
)
,function(err,numAffected) {
});
So each operation is performed in turn.