I have this table here:
Table: User
{
id: 1,
name: 'user 1',
superPowers: [
{
id: 1,
map: {
status: 'offline'
}
},
{
id: 2,
map: {
status: 'online'
}
},
{
id: 3,
map: {
status: 'online'
}
}
]
}
And I would like to return an object with the user information only containing superpowers that have the map in the online mode. I built this query:
await getRepository(User).createQueryBuilder('user')
.leftJoinAndSelect('user.superPowers', 'superPowers')
.leftJoinAndSelect('superPowers.map', 'map')
.where('map.status = status', {status: EnumStatusMap.ONLINE})
.andWhere('user.id = :id', {id: 1}).getOne();
But this query returns me all the supowerPowers, including the ones where the map status is offline
What am I doing wrong?
Related
First of all, I am using Algolia JavaScript API Client V3 (Deprecated)
I have the following records
{
category: SEDAN,
manufacturer: Volkswagen,
id: '123'
},
{
category: COUPE,
manufacturer: Renault,
id: '234'
},
{
category: SEDAN,
manufacturer: Fiat,
id: '345'
},
{
category: COUPE,
manufacturer: Peugeot,
id: '456'
},
{
category: SUV,
manufacturer: Volkswagen,
id: '567'
}
I want to query Algolia and get something similar to the following json
{
categories: {
SEDAN: {
count: 2
items: [{
Volkswagen: {
count 1,
items: [{
id: '123'
}]
}
},
{
Fiat: {
count 1,
items: [{
id: '345'
}]
}
}]
},
COUPE: {
count: 2
items: [{
Renault: {
count 1,
items: [{
id: '234'
}]
}
},
{
Peugeot: {
count 1,
items: [{
id: '456'
}]
}
}]
},
SUV: {
count: 1,
items: [{
Volkswagen: {
count 1,
items: [{
id: '567'
}]
}
}]
}
}
}
I have been trying to query Algolia
index
.search({
query: '',
facets: ['category', 'manufacturer'],
attributesToRetrieve: []
})
.then((result) => {
console.log(result.facets);
});
But I am not sure if it is possible to combine the facets
facets added to a query doesn't work that way. It will simply return the record count for each facet value, not the actual records (https://www.algolia.com/doc/api-reference/api-parameters/facets/)
You can create filters around facets and use those to display results by facet value, but there isn't a way to build a single response JSON that is already grouped by facets like you show above. https://www.algolia.com/doc/api-reference/api-parameters/filters/
I have the following code which nearly works. It will check if a status for a domain is already set, if not, it will push a new status. If there already is one, it will update it, unless there's multiple statuses for different domains stored, in which case it will replace all of the other documents for the other domains and not just the one entry for the specific domain.
if (Boolean(checkStat) === true) {
await userModel.findByIdAndUpdate(
{
_id: ctx.userId,
domain: args.domain,
},
{
["preferences.domain.status"]: {
domain: args.domain,
status: args.status,
},
},
);
return {
message:
"sucessfully updated status of " +
args.domain +
" to " +
args.status,
};
} else {
await userModel.findByIdAndUpdate(
ctx.userId,
{
$push: {
["preferences.domain.status"]: {
domain: args.domain,
status: args.status,
},
},
},
{
upsert: true,
new: true,
}
);
return {
message:
"sucessfully set status of " + args.domain + " to " + args.status,
};
}
preferences:
domain:
labels:
status:
0:
domain: "x.wtf"
status: "online"
_id: "Jz-ttsjEXKBSVxAN91CzD"
1:
domain: "a.lol"
status: "online"
_id: "Jz-ttsjEXKBSVxAN91CzD"
const userSchema = new mongoose.Schema({
preferences: {
domain: {
status: [
{
_id: {
type: String,
default: () => nanoid(),
},
domain: String,
status: String,
},
],
labels: [
{
_id: {
type: String,
default: () => nanoid(),
},
domain: String,
label: String,
},
],
},
},
});
You can use $ operator and findOneAndUpdate instead of findByIdAndUpdate to do that:
await userModel.findOneAndUpdate(
{
_id: ctx.userId,
"preferences.domain.status.domain": args.domain,
},
{
"preferences.domain.status.$.status": args.status,
}
);
I'm trying to update an array of id's instead of just one id but with no succes. I'm not even sure on how the payload data should look like.
This is my current request payload:
query: "mutation {
addStudentToGroup(
id: " [5c3b9884277bbd61dcdc174b,5c3b9884277bbd61dcdc174c]",
groupId: "5c3b974157905b2808373e3c"
)
{ id }
} "
And in my GraphQL I did this:
addStudentToGroup: {
type: StudentType,
args: {
id: {type: GraphQLList(GraphQLID)},
groupId: {type: GraphQLID}
},
resolve(parent, args) {
return Student.updateMany(
{ id: { $in: args.id} },
{ $set: { groupId: args.groupId }}
);
}
},
But what it returns to me is:
{"data":{"addStudentToGroup":{"id":null}}}
Why is this not working? It does work when I use findByIdAndUpdate
I'm trying to update an array of objects in my simple-schema. Currently, it removes everything in the database and leaves behind:
"careerHistoryPositions": []
My simple-schema looks like:
const ProfileCandidateSchema = new SimpleSchema({
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id
},
careerHistoryPositions: { type: Array, optional: true },
'careerHistoryPositions.$': { type: Object, optional: true },
'careerHistoryPositions.$.uniqueId': { type: String, optional: true },
'careerHistoryPositions.$.company': { type: String, optional: true },
'careerHistoryPositions.$.title': { type: String, optional: true }
});
If console.log form data looks like:
careerHistoryPositions: [Object, Object],
0: Object
company: "Test company"
title: "Test Title"
uniqueId: 1498004350350
1: Object
company: "Test company 2"
title: "Test Title 2"
uniqueId: 149800433221
My update function:
handleFormSubmit(event) {
event.preventDefault();
const { careerHistoryPositions } = this.state;
ProfileCandidate.update({ _id: this.state.profileCandidateCollectionId },
{ $set: {
careerHistoryPositions
}
}
);
}
I managed to fix this by mapping over my object and running 2 separate updates. The first removes the old element and the second adds the updated version. I'm sure there is a better way to do this, however, this does seem to work.
handleFormSubmit(event) {
event.preventDefault();
const { careerHistoryPositions } = this.state;
ProfileCandidate.update({_id: this.state.profileCandidateCollectionId}, { $unset: {
'careerHistoryPositions': {}
}
})
const updatePosition = this.state.careerHistoryPositions.map((position) => {
ProfileCandidate.update({_id: this.state.profileCandidateCollectionId}, { $push: {
'careerHistoryPositions': {
company: position.company,
title: position.title,
uniqueId: position.uniqueId
}
}
})
I'm newbie with MongoDB.
I have created a mapReduce on my Person collection to group cities.
db.Person.find()
[{
name: 'Bob',
addresses: [
{
street: 'Vegas Street',
neighborhood: {
name: 'Center',
city: {
name: 'Springfield'
}
}
}, {
.....
}
]
}, {
....
}]
And this is my mapReduce:
db.Person.mapReduce(function() {
for (var i = 0; i < this.address.length-1; i++) {
var address = this.address[i];
emit(address.neighborhood.city.name, 1);
}
}, function(k, v) {
return v.length;
}, { out: 'City' });
Then I use this to list my cities:
db.City.find().sort({'_id:', 1})
[{
_id: 'Springfield',
value: 3
}, {
_id: 'City B',
value: 2
}, {
...
}]
My question is about the City data, I need run the mapReduce each time I insert, update or delete on my Personcollection or it runs automatically?