swift firebase retrieve all object that the child of the object have the same key - swift

Below is my firebase and code , I would like to retrieve all activities which have the User John key inside User.
let ref = FIRDatabase.database().reference().child("activities/")
ref.queryOrderedByChild("User").queryEqualToValue("John").observeSingleEventOfType(.Value,
withBlock:{
(snapshot) in
for record in snapshot.children
{
}
})

This is not working because in your query you are trying to take all the activities having an attribute User == "John" when the real value of User is an Object like this:
{
"John": {
"age": 21
}
}
To solve this issue you should change your data structure creating a node where you save all your users, and keep in users/userID/activities only the id of the activities. For example, this would be a nicer way to structure your data:
{
"activities": {
"cycling": {
"users": {
"user1": true,
"user2": true
}
},
"running": {
"users": {
"user1": true
}
}
},
"users": {
"user1": {
"name": "John",
"age": 21,
"activities": {
"cycling": true,
"running": true,
}
},
"user2": {
"name": "Tim",
"age": 20,
"activities": {
"cycling": true
}
}
}
}
Then you can use
Let me know if this helped ;)

Related

Mongoose nested object not updating 'cannot create field "foo" in element'

I have a similar issue to this question.
I'm trying to create a new field using "findAndUpdate". I've tried all the methods, $set, $push, $addSet... none of them seem to be working and I keep getting the same error.
Here's the code:
router.post('/accept', auth, async (req, res) => {
const useremail = user.email
const originalEvent = await Event.findOneAndUpdate({eventId: 61469041, isOrganizer: true, "attendees.email": useremail},
{"$push":{"attendees.status" : "accepted"}},
{new: true})
res.status(200).json({originalEvent, event})
}
catch (e) {
res.status(400).json({ msg: e.message, success: false });
}
});
Here's the error code:
"Cannot create field 'status' in element {attendees: [ { _id: ObjectId('5f80a02a82dceb2810e0aa66'), email: "bob#gmail.com", name: "Bob" } ]}"
Here's the object I'm trying to update:
{
"organizer": {
"email": "alex#gmail.com",
"name": "Alex"
},
"_id": "5f80a02a82dceb2810e0aa65",
"title": "Go to the beach",
"eventId": 61469041,
"isOrganizer": true,
"user": "5f05f23417ca6ab69ccc4cf2",
"attendees": [
{
"_id": "5f80a02a82dceb2810e0aa66",
"email": "bob#gmail.com",
"name": "Bob"
}
],
"__v": 0,
}
Expected outcome:
{
"organizer": {
"email": "alex#gmail.com",
"name": "Alex"
},
"_id": "5f80a02a82dceb2810e0aa65",
"title": "Go to the beach",
"eventId": 61469041,
"isOrganizer": true,
"user": "5f05f23417ca6ab69ccc4cf2",
"attendees": [
{
"_id": "5f80a02a82dceb2810e0aa66",
"email": "bob#gmail.com",
"name": "Bob",
"status": "accepted"
}
],
"__v": 0,
}
SOLVED with this:
const originalEvent = await Event.findOneAndUpdate({eventId: eventId, "isOrganizer": true,
"attendees": {$elemMatch: {email: useremail}}
},
{ $set: { "attendees.$.status": "accepted"} }
)
res.status(200).json(originalEvent)
}
Referencing attendees.status doesn't make sense because in your schema attendees is not an object (with fields such as status) but an array. But you can do it differently. If you have the index of the attendee you want to mutate, you can do { $set: { "attendees.0.status": "accepted" } }, where 0 is the index in the array.
Also, with regards to the first half of your question, the error you're seeing is because $push works on arrays. So in order for your operation to work, you'd have to first initialize such an object {attendees: { status: [] } }.
If the field is not an array, the operation will fail. (docs)

group by properties and sum of values between nested json and array of objects

I have users array with their name,
var users = [{'name':'zulekha'}, {'name':'deepika'}];
I am fetching worklogged by each user from jira APIs. So I am getting object like this.
var worklogResult = {
"issues": [
{
"fields": {
"worklog": {
"worklogs": [
{
"author": {
"name": "zulekha",
},
"timeSpentSeconds": 180
},
{
"author": {
"name": "deepika",
},
"timeSpentSeconds": 210
}
]
}
}
},
{
"fields": {
"worklog": {
"worklogs": [
{
"author": {
"name": "deepika",
},
"timeSpentSeconds": 140
}
]
}
}
},
{
"fields": {
"worklog": {
"worklogs": [
{
"author": {
"name": "zulekha",
},
"timeSpentSeconds": 600,
}
]
}
}
},
{
"fields": {
"worklog": {
"worklogs": []
}
}
}
]
}
Now I want to match worklogResult with users in such a way that I can get following output.
output = [{'name':'zulekha','timeSpentSeconds':780}, {'name':'deepika', 'timeSpentSeconds':350}]
Can anyone suggest me how to achieve this?
use _.flatMap to flat nested objects
_.chain(worklogResult.issues)
.flatMap('fields.worklog.worklogs')
.thru(function(spents) {
return _.map(users, function(user) {
return _.merge(user, {
timeSpentSeconds: _.chain(spents)
.filter(['author.name', user.name])
.map('timeSpentSeconds')
.sum()
.value()
})
})
})
.value()

Finding multiple docs using same id not working, using meteor + react and mongoDB

How do I get the email address of the students in the same class_id, take it as there are more then 2 students in different class in the DB as well?
I have this but it return empty array []
Meteor.users.find({"course_learn_list.$.class_id": {$in: [classId]}},
{field: {"emails.address": 1}}
).fetch()
Collections
{
"_id": "LMZiLKs2MRhZiiwoS",
"course_learn_list": [
{
"course_id": "M8EiKfxAAzy25WmFH",
"class_id": "jePhNgEuXLM3ZCt98"
},
{
"course_id": "5hbwrfbfxAAzy2nrg",
"class_id": "dfbfnEuXLM3fngndn"
}
],
"emails": [
{
"address": "student1#gmail.com",
"verified": false
}
]
},
{
"_id": "JgfdLKs2MRhZJgfNgk",
"course_learn_list": [
{
"course_id": "M8EiKfxAAzy25WmFH",
"class_id": "jePhNgEuXLM3ZCt98"
},
{
"course_id": "5hbwrfbfxAAzy2nrg",
"class_id": "dfbfnEuXLM3fngndn"
}
],
"emails": [
{
"address": "student2#gmail.com",
"verified": false
}
]
}
I think you want:
Meteor.users.find({ "course_learn_list.class_id": classId },
{ "course_learn_list.$": 1, "emails.address": 1 }).fetch()
This should find the first instance in each course_learn_list array where the classId is your classId.
In this case you probably don't need to use a projection to get the right answer. Here's an example of extracting the verified email addresses using only the . operator in the selector:
const ids = ['jePhNgEuXLM3ZCt98', 'some-other-id'];
const emails =
Meteor.users.find({ 'course_learn_list.class_id': { $in: ids } })
.fetch()
.map(user => _.findWhere(user.emails, { verified: true }).address);
This works for me!
Meteor.publish("getMyClassStudents", function(classId) {
console.log("Publish getMyClassStudents")
var self = this
if (self.userId) {
var data = Meteor.users.find({
"course_learn_list.class_id": classId
}, {
"fields": {
"emails.address": 1
}
})
return data
}
else {
return self.ready()
}
})

auto-increment using loopback.js and MongoDB

i want to increase mongodb document number automatically using loopback.
I made function in mongo
function getNextSequence(name) {
var ret = db.counters.findAndModify(
{
query: { _id: name },
update: { $inc: { seq: 1 } },
new: true
}
);
return ret.seq;
}
db.tweet.insert(
{
"_id" : getNextSequence("userid"),
"content": "test",
"date": "1",
"ownerUsername": "1",
"ownerId": "1"
}
)
It is working in mongo shell.
However when I insert using loopback.js browser (http://localhost:3000/explorer/), It is not working.
400 error(SytaxError) code is showing.
I can not use mongo function in loopback rest API ?
I think problem is quotes in this line getNextSequence("userid"),
Create a collection counters with properties value and collection
{
"name": "counters",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"type": "number",
"collection": "string"
},
"validations": [],
"relations": {},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": []
}
Now supposing your auto-increment collection name tweets.
Insert this value to counters.
{
"value" : 0,
"collection" : "tweet"
}
Now common/models/tweet.js
tweet.observe('before save', function (ctx, next) {
var app = ctx.Model.app;
//Apply this hooks for save operation only..
if(ctx.isNewInstance){
//suppose my datasource name is mongodb
var mongoDb = app.dataSources.mongodb;
var mongoConnector = app.dataSources.mongodb.connector;
mongoConnector.collection("counters").findAndModify({collection: 'tweet'}, [['_id','asc']], {$inc: { value: 1 }}, {new: true}, function(err, sequence) {
if(err) {
throw err;
} else {
// Do what I need to do with new incremented value sequence.value
//Save the tweet id with autoincrement..
ctx.instance.id = sequence.value.value;
next();
} //else
});
} //ctx.isNewInstance
else{
next();
}
}); //Observe before save..
I would love to add 1 more point to Robins Answer,you can add upsert:true so that it automatically creates the document if it doesn't exist
tweet.observe('before save', function (ctx, next) {
var app = ctx.Model.app;
//Apply this hooks for save operation only..
if(ctx.isNewInstance){
//suppose my datasource name is mongodb
var mongoDb = app.dataSources.mongodb;
var mongoConnector = app.dataSources.mongodb.connector;
mongoConnector.collection("counters").findAndModify({collection: 'tweet'}, [['_id','asc']], {$inc: { value: 1 }}, {new: true,upsert:true}, function(err, sequence) {
if(err) {
throw err;
} else {
// Do what I need to do with new incremented value sequence.value
//Save the tweet id with autoincrement..
ctx.instance.id = sequence.value.value;
next();
} //else
});
} //ctx.isNewInstance
else{
next();
}
}); //Observe before save..
You can do something like in this example for loopback 4
let last_record = await this.testRepository.findOne({order: ['id DESC']});
if(last_record) invoice.id = last_record.id+1;
This will generate your model with the property:
#property({
type: 'number',
id: true,
default: 1,
generated: false
})
id: number;
Hopefully, this helps, please write me if there is any other code. Thanks
If you want to use MongoDB operators directly in loopback methods you need to enable the option "allowExtendedOperators", you can do so on a per model basis or at the data source level (will apply to all models using the data source).
datasources.json:
"MongoDs": {
"host": "127.0.0.1",
"port": 27017,
"url": "mongodb://localUser:MYPASSWORD!#127.0.0.1:27017/test-database",
"database": "test-database",
"password": "MYPASSWORD!",
"name": "MongoDs",
"user": "localUser",
"useNewUrlParser": true,
"connector": "mongodb",
"allowExtendedOperators": true
},

MongoDB: How to find by subdocument ID?

I am trying to model the concept of games where teams of players compete against each other in MongoDB.
I have two collections: players and games.
This is how a document in games looks like.
{
"_id": { "$oid": "1" },
"teams": [
{
"players": [
{
"player": { "$oid": "2" },
"score": 500,
},
{
"player": { "$oid": "3" },
"score": 550,
}
]
},
{
"players": [
{
"player": { "$oid": "4" },
"score": 500,
},
{
"player": { "$oid": "5" },
"score": 550,
}
]
}
]
}
Here's the task: given a player ID I want to find all games in which this player participated.
What I've tried:
db.games.find( { "teams.players.player._id": "2" } )
However, this does not return anything.
By the way, I'm using Mongoose with the following schema:
playerSchema = Schema
player: { type: Schema.ObjectId, ref: 'Player' }
score: { type: Number }
teamSchema = Schema
players: [ playerSchema ]
gameSchema = Schema
teams: [ teamSchema ]
with the following CoffeeScript query:
Game.find 'teams.players.player._id': playerId
which returns no results for any player ID.
In your document:
"players": [
{
"player": { "$oid": "4" },
"score": 500,
},
{
"player": { "$oid": "5" },
"score": 550,
}
]
The player field in the embedded collection of players is a BSON Id (i.e. it looks something like ObjectId("4e208e070347a90001000008")), so I think you should structure your query like so:
db.games.find( { "teams.players.player": ObjectId("2") } )
Note, I've dropped the _id -- provided that works in a mongo console, then I suspect the Coffee query will be similar (drop the _id portion).
I used your feedback to restrict this example to a home and away team. Remember that Mongo is not a relational database and using relationships is often an indication of a relational-database mindset. Duplication is the key.
Instead of references from the game to specific player documents, I have stored the name of the player. I assume this is an immutable unique index. The player has been given a games array containing references to each game he played in. This is actually quite bad as we have to query again and populate this array. It would be better to store a name here as well, but since I don't know your situation I'm not sure if a game has an immutable representable name.
This basic idea has to be improved with error checking (e.g. middleware) but I leave that up to you.
// Schemas
var playerSchema = new Schema({
name: {type: String, index: true, unique: true},
games: {type: [Schema.ObjectId], ref: 'Game'}
});
var gameSchema = new Schema({
homeTeam: [{player: {name: String}, score: Number}]
awayTeam: [{player: {name: String}, score: Number}]
});
// Models
var Player = mongoose.model('Player', playerSchema);
var Team = mongoose.model('Team', teamSchema);
var Game = mongoose.model('Game', gameSchema);
// Middleware
gameSchema.post('save', function (game) {
var addMatchToPlayer = function(name) {
Player.findOne({name: name}, function(err, player) {
if (!err && player && player.games.indexOf(game._id) === -1) {
player.games.push(game._id);
player.save();
}
});
}
for (var i = 0; i < game.homeTeam.length; i++) {
addMatchToPlayer(game.homeTeam[i].name);
}
for (var i = 0; i < game.awayTeam.length; i++) {
addMatchToPlayer(game.awayTeam[i].name);
}
});
// Queries
Player.findOne({name: 'Roel van Uden'}).populate('games').exec(function (err, player) {
});