I have two collections:
users:
{
_id: ObjectId('123...'),
docs: [
ObjectId('512d5793abb900bf3e000002'),
ObjectId('512d5793abb900bf3e000001')
]
}
docs:
{
_id: ObjectId('512d5793abb900bf3e000002'),
name: 'qwe',
...
}
{
_id: ObjectId('512d5793abb900bf3e000001'),
name: 'qwe2',
...
}
I want to get docs from ids. I try this solution, but I get this message:
{ db: { domain: null,
_events: {},
_maxListeners: 10,
databaseName: 'test', ...
Your message looks like a mongodb cursor returned from find by native mongodb driver.
To get actual data you should use toArray function of the cursor:
var ObjectID = require('mongodb').ObjectID;
// you shall wrap each id in ObjectID
var idsProjects = [
ObjectID('512d5793abb900bf3e000002'),
ObjectID('512d5793abb900bf3e000001')
];
collectionProjects.find({
_id: { $in: idsProjects }
},{
_id: -1, // use -1 to skip a field
name: 1
}).toArray(function (err, docs) {
// docs array here contains all queried docs
if (err) throw err;
console.log(docs);
});
But I recommend you to switch from native mongodb driver to some wrapper around it like monk.
If you care about the order of the list, the answer of Mr.Leonid may not work as expected to do.
That's because find gets the docs that have _id equals to any _ids $in the list so the output docs will be ordered by the main order of the collection itself not the order of the input list.
To solve that you can just use the normal findOne with a for loop to the list.
The code will look like:
var ObjectID = require('mongodb').ObjectID;
var idsProjects = [
'512d5793abb900bf3e000002',
'512d5793abb900bf3e000001'
];
let usersList = new Array();
for (let index = 0; index < idsProjects.length; index++) {
const myID = idsProjects[index];
const query = { _id: ObjectID(myID) };
const options = {
projection: {name: 1 };
var user= await collectionProjects.findOne(query,options);
usersList.push(user);
}
// that's it,
// here we have a list of users 'usersList'
//with same order of the input ids' list.
console.log(usersList);
Related
This is my ObjectIds array -
obj_ids = [
"5ee71cc94be8d0180c1b63db",
"5ee71c884be8d0180c1b63d9",
"5ee71c494be8d0180c1b63d6",
"5ee71bfd4be8d0180c1b63d4"
]
I am using these objectids to serach whether they exist in the db or not and based on that I want to send the response to server.
This is the code I am trying but I dont know how to populate the array and send it to the server.
var msg = [];
obj_ids.map((ele) => {
Lead.find({ _id: ele._id }, async function (error, docs) {
if (docs.length) {
msg.push(
`Lead already exist for Lead id - ${ele._id} assgined to ${docs[0].salesPerson}`
);
} else {
msg.push(`Lead doesn't exist for Lead id: ${ele._id}`);
const newDuty = new AssignedDuty({
duty: ele._id,
salesPerson: req.body.salesPerson,
});
await newDuty.save();
}
});
});
res.json(msg);
By doing this approach I am getting an empty array. I cannot put res.json(msg) inside the loop. If it is possible by using async-await, please guide me through.
You don't need to make multiple queries to find whether given object ids exist in the database.
Using $in operator, you can make one query that will return all the documents where the _id is equal to one of the object id in the list.
const docs = await Lead.find({
_id: {
$in: [
"5ee71cc94be8d0180c1b63db",
"5ee71c884be8d0180c1b63d9",
"5ee71c494be8d0180c1b63d6",
"5ee71bfd4be8d0180c1b63d4"
]
}
});
After this query, you can check which object id is present in the docs array and which is absent.
For details on $in operator, see $in comparison operator
Your code can be simplified as shown below:
const obj_ids = [
"5ee71cc94be8d0180c1b63db",
"5ee71c884be8d0180c1b63d9",
"5ee71c494be8d0180c1b63d6",
"5ee71bfd4be8d0180c1b63d4"
];
const docs = await Lead.find({
_id: { $in: obj_ids }
});
const msg = [];
obj_ids.forEach(async (id) => {
const doc = docs.find(d => d._id == id);
if (doc) {
msg.push(
`Lead already exist for Lead id - ${doc._id} assgined to ${doc.salesPerson}`
);
}
else {
msg.push(`Lead doesn't exist for Lead id: ${id}`);
const newDuty = new AssignedDuty({
duty: id,
salesPerson: req.body.salesPerson
});
await newDuty.save();
}
});
res.json(msg);
I have a mongoDB database containing Events. Each event is defined by a number of fields, including an array of genres (which are subdocuments consisting of a {genre and subGenre}).
For example: an event could be of {genre: "music", subGenre: "jazz"}, and {genre: "music", subGenre: "blues"}. See event.js - the model. In this case, 2 "genre" sub documents are added to the "genres" array in the Event document in the database.
Back in my node application - see query.ts - I am trying to work out how to run a query that lets the user search for all events that match their genre preferences.
So:
the Event is defined by an array of genres (in the database), and
the user's preferences are defined by an array of genres (in the
application).
I am looking to define a mongoDB query that returns all Events where there is a match of any 1 {genre, subGenre} combination between the 2 arrays.
I looked into $in Query Selector in the mongoDB documentation and suspect it might need to be used... but programmatically, how do I write a query that expands to include all the values in the variable "searchGenres" in query.ts?
Thanks a lot in advance for your thoughts.
event.js: mongoDB Model for 'Events" defined using Mongoose - snippet:
let mongoose = require('mongoose');
let EventSchema = new mongoose.Schema({
genres: [
{
genre: String,
subGenre: String
}
]
)};
module.exports = mongoose.model('Event', EventSchema);
query.ts:
import mongoose = require('mongoose');
let Event = require ('../models/event');
class Genre {
genre: string;
subGenre: string;
constructor (gen: string, sub: string) {
this.genre = gen;
this.subGenre = sub;
}
}
async function runQuery()
{
let searchGenres : Array<Genre> = new Array<Genre>();
// populate searchGenres with some data here... e.g.
const searchGenre1 : Genre = new Genre ('music', 'jazz');
const searchGenre2 : Genre = new Genre ('music', 'rock');
searchGenres.push(searchGenre1);
searchGenres.push(searchGenre2);
// Query logic here:
// Return all events from the database, if the 'genres' array
// in the document matches any element in the searchGenres array
// defined above..
const events = await Event.find ({
'genres': {?help? }
});
}
```
After some self-education today, I have come up with a solution, which I am happy with - to append to end of query.ts:
type GenreQuerySelector = { genres: { $elemMatch: { 'genre': string; 'subGenre': string; }; }; };
let querySelectors : Array< GenreQuerySelector > = new Array< GenreQuerySelector >();
for (let genre of searchGenres) {
const genreQuery : GenreQuerySelector = {
genres: {
$elemMatch: {
'genre': genre.genre,
'subGenre': genre.subGenre
}
}
};
querySelectors.push(genreQuery);
};
const events = await Event.find ({ $or: querySelectors }).exec();
I am trying to add a new key:value pair to an existing object of mongoDB document, but no steps are helping me
I tried $each, $push $addtoset but i understood those are for arrays then i tried $det but it is updating the existing key:value pair with new key:value pair
Here is my document
{
test:"abc",
test2:"cdf",
test3:{ 1:"one"}
}
if you observer test3 key in the above document i have already 1:"one" now i want to add new key value in the same object
Like
{
test:"abc",
test2:"cdf",
test3:{ 1:"one", 2:"two", 3:"three"}
}
is it possible in mongoDB?
Here is the mongo Query
let val = parseInt(DYNAMICVALUE)
var changfeemaildot = (req.session.email).replace(/\./g, '#')
var seld = {
_id: ObjectId(rx[0]._id)
};
var seldu = {
$set:{
emails: {
[changfeemaildot]: val
}
}
};
var collection =
connection.get().collection('problems');
collection.update(seld, seldu, function (err, rail) {
});
You can use $set. So your code can be something like this
db.collection.update({<your_condition>}, {$set: {"test3.2": "two", "test3.3": "three"}});
In your case, it will be something like this
var seldu = {$set: {["emails." + changfeemaildot]: val}}
You can use $set with findOneAndUpdate. So your code can be something like this
const { Types, connection } = require("mongoose");
const productList = await connection.collection('products').find({}).toArray()
productList.forEach(async function(myDoc) {
await connection.collection('products').
updateOne({ productId: Types.ObjectId(myDoc.productId) }, {
$set: {
productDisplayId: 'anything you want'
}
});
});
I need to query for 2 dynamic properties using the node.js driver for mongodb.
Here's the data structure:
{
"_id":"123456",
"dateAdded":"2017-09-20T08:36:40.325Z",
"followers":{
"name1":{
"followedOn":"2017-09-20T08:36:40.325Z",
"unfollowedOn":null
},
"name2":{
"followedOn":"2017-09-20T08:36:40.325Z",
"unfollowedOn":null
}
}
}
Here's my code:
//Working but not dynamic
collections.find({ '_id': '123456', 'followers.name1': { $exists: false } })
//My failed attempt at making it dynamic
const id = "123456"
const username = "name1"
let query = {}
query['followers.'+username] = { $exists: true }
collections.find( { "_id": id, query }
Note that this is not a duplicate of "how to make a dynamic key in an object literal". The .find() method of node.js mongodb driver does not accept object literals. I can't find documentation of what it accepts exactly.
Your _id property needs to be within query object, not separate.
Here's how to do it:
let query = { _id: id };
query['followers.'+username] = { $exists: true }
collections.find(query);
I am trying to update a MongoDB collection that I have created a schema for using Mongoose. My goal is to remove all elements in an array known as 'alarms' that are less than (in the past) of the current time.
Here is my Mongoose Schema:
var mongoose = require('mongoose');
var ReminderSchema = new mongoose.Schema({
firstName: String,
lastName: String,
phone: Number,
email: String,
medication: String,
alarms: [Date]
});
module.exports = mongoose.model('Reminder', ReminderSchema);
Here is the update. I am trying to remove all alarms that are less than the current date.
var date = new Date();
var Reminder = require('./models/Reminders.js');
//remove ALL alarms that are are before current time
Reminder.update({alarms: {$lte: date}}, {$pullAll: {alarms: {$lte: date}}}, function(err, updated){
if(err){
console.log(err);
}
else {
console.log(updated);
}
});
I am currently getting this error:
{ [MongoError: $pullAll requires an array argument but was given a Object]
name: 'MongoError',
message: '$pullAll requires an array argument but was given a Object',
driver: true,
index: 0,
code: 2,
errmsg: '$pullAll requires an array argument but was given a Object' }
Any thoughts?
The $pullAll receives an array not an object:
{ $pullAll: { <field1>: [ <value1>, <value2> ... ], ... } }
The $pullAll operator removes all instances of the specified values
from an existing array. Unlike the $pull operator that removes
elements by specifying a query, $pullAll removes elements that match
the listed values.
the solution you should use $pull :
Reminder.update({alarms: {$lte: date}},{$pull:{alarms: { $lte : new Date()}}},
function(err, updated){
if(err){
console.log(err);
}
else {
console.log(updated);
}
});
https://docs.mongodb.org/manual/reference/operator/update/pullAll/