I have one basic User schema
var UserSchema = new Schema({
name: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
Now I want to implement dual login feature,
async.parallel([
function (cb) {
User.findOne({$and: [{name: req.body.username1}, {password: req.body.password1}]}, function (err, u) {
if (!u)
err = "User1 dose not exist";
cb(err, u)
});
},
function (cb) {
User.findOne({$and: [{name: req.body.username2}, {password: req.body.password2}]}, function (err, u) {
if (!u)
err = "User2 dose not exist";
cb(err, u)
});
}
], function (err, results) {
....
I want to whether there is simple way to find those two user information in one User.find() function?
You just use $or. Also your use of $and is redundant here:
User.find({
"$or": [
{ "name": req.body.username1, "password": req.body.password1 },
{ "name": req.body.username2, "password": req.body.password2 }
]
},function(err,result) {
// logic in here
})
And then process whatever logic you need to validate the response, with the clearest case being that if the response is not at least two items long then one of the selections was not found.
This is one of those cases where of course "username" should have a unique constraint.
Related
const userSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
posts: [{
type: Schema.Types.ObjectId,
ref: 'Post'
}],
friends: [{
type: Schema.Types.ObjectId,
ref: 'User'
}],
});
// Exporting the schema so it can be accessed by requiring it.
module.exports = mongoose.model('User', userSchema);
As you can see I got this user schema that has a friends array and a posts array.
User.findById(userId).then(result => {
Post.find(query).then(posts => {
res.status(200).json(posts)
}).catch(err => {
if (!err.statusCode) {
err.statusCode = 500;
}
next(err);
})
});
Is there any query that can fit in the find() above in order to get all the posts of the user's friends?
If in the post model you have a link to the user model, that is, some field that identifies who wrote the post, you could use a for loop to search for posts made by the user's friends.
I don't know if this is the best solution but I hope it helps.
As a tip, you should use asynchronous syntax instead of promises, this helps when correcting errors.
async function getFriendsPosts(req,res){
/*in this array we will store the
posts of the user's friends */
let posts = [];
try{
//we check if the user exists
let user = User.findById(req.params.id);
//if it doesn't exist we will send a message
if(!user) res.status(404).send("User not Found");
else{
/* here we compare the id of the friends with the id of
the friends with the "creator" field in the post model*/
for await(let friend of user.friends){
for await(let creator of Post.find()){
/* if there is a match we send
it to the post array*/
if(friend._id.equals(creator._id)){
posts.push(creator);
}
}
}
/*finally we send the array with the posts*/
res.send(posts);
}
}catch(err){
res.status(500).send("Internal Server Error");
}
}
If I suppose that the Post Schema is something like that
{
title: String,
content: String,
owner: { type: Schema.Types.ObjectId, ref: 'User'}
}
then we can use aggregate pipeline to get the friends posts of some user
something like that
db.users.aggregate([
{
$match: {
_id: "userId1" // this should be of type ObjectId, you need to convert req.params.id to ObjectId (something like: mongoose.Types.ObjectId(req.params.id) instead of 'userId1')
}
},
{
$lookup: {
from: "posts",
let: {
friendsIDs: "$friends"
},
pipeline: [
{
$match: {
$expr: {
$in: ["$owner", "$$friendsIDs"]
}
}
}
],
as: "friendsPosts"
}
}
])
you can test it here Mongo Playground
feel free to replace these 'userId1', 'userId2', ..., 'postId1, 'postId2', .. in this link with your real users and posts Ids
by this way, you got the friends posts of some user in one query rather than two queries
then the function will be something like that
User.aggregate([
{
$match: {
_id: mongoose.Types.ObjectId(req.params.id)
}
},
{
$lookup: {
from: "posts", // this should be the posts collection name, It may be 'Post' not 'posts', check it
let: {
friendsIDs: "$friends"
},
pipeline: [
{
$match: {
$expr: {
$in: ["$owner", "$$friendsIDs"]
}
}
}
],
as: "friendsPosts"
}
}
]).then(result => {
// the aggregate pipeline is returning an array
// but we are sure it will be an array of only one element as we are searching for only one user, so we can use result[0]
result = result || []; // double check the result array
result[0] = result[0] || {}; // double check the user object
var posts = result[0].friendsPosts; // here is the friends posts array
// return the posts array
res.json(posts);
})
hope it helps
Update
If we need to sort the firendsPosts, and then limit them
we can use the following
db.users.aggregate([
{
$match: {
_id: "userId1"
}
},
{
$lookup: {
from: "posts",
let: {
friendsIDs: "$friends"
},
pipeline: [
{
$match: {
$expr: {
$in: [
"$owner",
"$$friendsIDs"
]
}
}
}
],
as: "friendsPosts"
}
},
{
$unwind: "$friendsPosts" // unwind the array to get a stream of documents
},
{
$sort: {
"friendsPosts.createdAt": 1 // then sort the posts by the createdAt Date in ascending order
}
},
{
$group: { // then group the posts again after sorting
_id: "$_id",
friendsPosts: {
$push: "$friendsPosts"
}
}
},
{
$project: {
friendsPosts: {
$slice: ["$friendsPosts", 2] // this is to limit the posts
}
}
}
])
you can test it here Mongo Playground 2
I have an object that looks like this.
{
_id: '577fe7a842c9b447',
name: 'Jacob\'s Bronze Badges',
competitors: [
{
_id: '577fe7a842c9bd6d',
name: 'Peter\'s Silver Badges',
sites: [
{
_id: '577fe7a842c9bd6d',
name: 'Facebook',
url: 'fb.com/peter'
},
{
_id: '577fe7a842c9bd6d'
name: 'Google',
url: 'google.com/peter'
}
]
},
{
_id: '599fe7a842c9bd6d',
name: 'Paul\'s Gold Badges',
sites: [
{
'_id': '577fe7a842c9bd6d',
name: 'Facebook',
url: 'fb.com/paul'
},
{
_id: '577fe7a842c9bd6d',
name: 'Google',
url: 'google.com/paul'
}
]
}
]
}
My goal is to reference the competitors array and update items inside with all of the values from req.body. I based this code off of this answer, as well as this other one.
Location.update(
{ 'competitors._id': req.params.competitorId, },
{ $set: { 'competitors.$': req.body, }, },
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
I send my HTTP PUT to localhost:3000/competitors/577fe7a842c9bd6d to update Peter's Silver Badges. The request body is:
{
"name": "McDonald's"
}
The problem is that when I use $set to set the competitor with _id: req.params.competitorId, I don't know what is in req.body. I want to use the entire req.body to update the object in the array, but when I do, that object is overwritten, so instead of getting a new name, Peter's Silver Badges becomes:
{
name: 'McDonald\'s',
sites: []
}
How can I update an object within an array when I know the object's _id with all of the fields from req.body without removing fields that I want to keep?
I believe that the sites array is empty because the object was reinitialized. In my schema I have sites: [sitesSchema] to initialize it. So I am assuming that the whole competitors[_id] object is getting overwritten with the new name and then the sites: [sitesSchema] from myschema.
You would need to use the $ positional operator in your $set. In order to assign those properties dynamically, based on what is in your req.body, you would need to build up your $set programmatically.
If you want to update the name you would do the following:
Location.update(
{ 'competitors._id': req.params.competitorId },
{ $set: { 'competitors.$.name': req.body.name }},
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
One way you might programatically build up the $set using req.body is by doing the following:
let updateObj = {$set: {}};
for(var param in req.body) {
updateObj.$set['competitors.$.'+param] = req.body[param];
}
See this answer for more details.
To update embedded document with $ operator, in most of the cases, you have to use dot notation on the $ operator.
Location.update(
{ _id: '577fe7a842c9b447', 'competitors._id': req.params.competitorId, },
{ $set: { 'competitors.$.name': req.body.name, }, },
(err, result) => {
if (err) {
res.status(500)
.json({ error: 'Unable to update competitor.', });
} else {
res.status(200)
.json(result);
}
}
);
I'm trying to get some data from a MongoDB database with the find() method, returning only those documents that contain a specified "room". Then, I want to return all distinct values, of the found array of rooms, whose key is equal to "variety". I tried this in two different ways and I could be way off in my approach. The first way was to chain the collection methods find() and distinct(). This did not work:
This is what the plantList collection looks like:
[
{
"_id": {
"$oid": "56c11a761b0e60030043cbae"
},
"date added": "10/21/2016",
"variety": "Lettuce",
"room": "Room 1"
},
{
"_id": {
"$oid": "56c11a761b0e60030043cbaf"
},
"date added": "10/21/2015",
"variety": "Tomatoes",
"room": "Room 2"
}
]
server.js
//plantList = db collection
var mongojs = require('mongojs');
var MongoClient = require("mongodb").MongoClient;
MongoClient.connect(process.env.MONGOLAB_URI, function(err, db) {
var plantList = db.collection("plantList");
app.get('/gettypesbyroom/:room', function(req, res) {
var roomReq = req.params.room;
plantList
.find({"room":roomReq})
.toArray()
.distinct("variety", function(err, docs) {
if (err) throw err;
res.json(docs);
});
});
});
My second approach was to chain promises with .then() and use underscore.js to select the keys of the array of rooms (also did not work):
app.get('/gettypesbyroom/:room', function(req, res) {
var roomReq = req.params.room;
plantList
.find({"room":roomReq})
.toArray(function(err, docs) {
if (err) throw err;
return docs;
}).then(function(docs) {
_.keys(docs, function(docs) { return docs.variety; });
}).then(function(varieties) {
res.json(varieties); //not inside scope of .get function?
});
});
Is there something I could do differently to make these work or perhaps a different approach altogether?
Try calling it without toArray:
//plantList = db collection
app.get('/gettypesbyroom/:room', function(req, res) {
var roomReq = req.params.room;
plantList
.find({ room: roomReq })
.distinct('type', function(err, docs) {
if (err) throw err;
res.json(docs);
});
});
See How Do I Query For Distinct Values in Mongoose.
You can do it two more ways. One is to make it easier with a projection operator - just project what you need. Eg. if you have a document that looks like:
{
room: 123,
type: 'soundproof_room',
other: 'stuff'
}
...you can project it with a query like this to just select the type:
db.rooms.find({ room: room }, { type: 1 }).toArray();
That would give you an array of objects like this:
let roomTypes = [{type: 'soundproof_room'}, {type: 'windy_room'}, {type: 'soundproof_room'}, {type: 'room without doors'}]
(Obviously, I'm making types up, I don't know what they really are.)
And then use a simple map:
return res.json(
roomTypes
// extract the type
.map(room => room.type)
// filter out duplicates
.filter((type, idx, self) => self.indexOf(type) === idx)
);
(I'm using ES6 arrow fn, hope you can read it, if not: babeljs.io/repl/)
Another thing to try is an aggregation.
db.rooms.aggregate({
// first find your room
$match: { room: room }
},
{
// group by type, basically make distinct types
$group: {
_id: '$type',
count: {$sum: 1} // inc this by 1 for each room of this type
}
});
That one would get you your room, and it would return you only the types of rooms and the count per type, as an added bonus.
i am trying to toggle the boolean value inside an array which is a collection of objects, problem is that field is being triggered for two both objects inside array, and i want to toggle it for a one object only.
Document:
"Invitation" : [
{
"__v" : 0,
"ID" : ObjectId("54afaabd88694dc019d3b628"),
"__t" : "USER",
"_id" : ObjectId("54b5022b583973580c706784"),
"Accepted" : false
},
{
"__v" : 0,
"ID" : ObjectId("54af6ce091324fd00f97a15f"),
"__t" : "USER",
"_id" : ObjectId("54bde39cdd55dd9016271f14"),
"Accepted" : false
}
]
Controller:
User.find({_id: req.user._id},'Invitation',function(err,docs) {
if (err) {
console.log(err);
}
var results = [];
async.each(docs,function(doc,callback) {
async.each(doc.Invitation,function(invite,callback) {
User.findOneAndUpdate(
{'_id': doc._id, 'Invitation._id': invite._id},
{'$set': {'Invitation.$.Accepted': !invite.Accepted}},
function(err,doc) {
results.push(doc);
callback(err);
}
);
},callback);
},function(err) {
if (err)
console.log(err);
console.log('end'+results);
});
});
MORE:
ID field inside Invitation array is objectId of person, let's say Bob and David send Invitations, so there are two objects inside Invitation array, that means i have two Invitations from two different person, i.e ( Bob and David) now i want to accept invitation of Bob only, so when i accept invitation of Bob, Accepted field should be triggered as true of Bob object in database, now the results that are shown in below answer have both objects set to true, where i only want accepted invitation to be true.
Same is happening with me when i accept invitation of only one user/person both objects are getting true.
I cannot see the result that you claim, which means it works for me and the rest of the world. Here is the abridged listing as a single script example:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/test');
var invitationSchema = new Schema({
"ID": { "type": Schema.Types.ObjectId },
"Accepted": Boolean
});
var userSchema = new Schema({
"Invitation": [invitationSchema]
});
var User = mongoose.model( 'User', userSchema );
User.find({},'Invitation',function(err,docs) {
if (err) throw err;
var results = [];
async.each(docs,function(doc,callback) {
async.each(doc.Invitation, function(invite,callback) {
User.findOneAndUpdate(
{ "_id": doc._id, "Invitation._id": invite._id },
{ "$set": { "Invitation.$.Accepted": !invite.Accepted } },
function(err,doc) {
results.push(doc);
callback(err);
}
);
},callback);
},function(err) {
if (err) throw err;
console.log( results.slice(-1)[0] );
process.exit();
});
});
So that clearly "toggles" both values as requested and works perfectly.
This is the result from me on one shot:
{ _id: 54be2f3360c191cf9edd7236,
Invitation:
[ { __v: 0,
ID: 54afaabd88694dc019d3b628,
__t: 'USER',
_id: 54b5022b583973580c706784,
Accepted: true },
{ __v: 0,
ID: 54af6ce091324fd00f97a15f,
__t: 'USER',
_id: 54bde39cdd55dd9016271f14,
Accepted: true } ] }
I got 3 database models in mongoose that looks like this:
//profile.js
var ProfileSchema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true },
matches: [{ type: Schema.Types.ObjectId, ref: 'Match' }]
});
//match.js
var MatchSchema = new Schema({
scores: [{ type: Schema.Types.ObjectId, ref: 'Score', required: true }],
});
//score.js
var ScoreSchema = new Schema({
score: {type: Number, required: true},
achivement: [{type: String, required: true}],
});
And I try to populate a profile with
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
.populate('matches')
.populate('matches.scores')
.exec(function(err, profile) {
if (err) {...}
if (profile) {
console.log(profile);
}
});
The matches get populated but I dont get the scores in matches to populate. Is this not supported in mongoose or do I do something wrong? Populate gives me this:
{
user_token: "539b07397c045fc00efc8b84"
username: "username002"
sex: 0
country: "SE"
friends: []
-matches: [
-{
__v: 1
_id: "539eddf9eac17bb8185b950c"
-scores: [
"539ee1c876f274701e17c068"
"539ee1c876f274701e17c069"
"539ee1c876f274701e17c06a"
]
}
]
}
But I want to populate the score array in the match array. Can I do this?
Yes, you are right. I tried using Chaining of populate I got same output.
For your query please use async.js and then populate by the method mentioned below.
For more details please have a look at this code snippet. It is a working, tested, sample code according to your query. Please go through the commented code for better understanding in the code below and the link of the snippet provided.
//Find the profile and populate all matches related to that profile
Profile.findOne({
_id: mongoose.Types.ObjectId(profile_id)
})
.populate('matches')
.exec(function(err, profile) {
if (err) throw err;
//We got the profile and populated matches in Array Form
if (profile) {
// Now there are multiple Matches
// We want to fetch score of each Match
// for that particular profile
// For each match related to that profile
async.forEach(profile.matches, function(match) {
console.log(match, 'match')
// For each match related to that profile
// Populate score achieved by that person
Match.find({
_id:match.id
})
.populate('scores', 'score')
.exec(function (err, score) {
if (err) throw err;
// here is score of all the matches
// played by the person whose profile id
// is passed
console.log(score);
})
})
}
});
Profile.findOne({ _id: mongoose.Types.ObjectId(profile_id) })
.populate('matches.scores')
.exec(function(err, profile) {
if (err) {...}
if (profile) {
console.log(profile);
}
});