MongoDB: find if ID exist in array of objects - mongodb

I was wondering, is there a way in MongoDB to get all the documents from one collection excluding those that exists in another, without using aggregation.
Let me explain myself more;
I have a collection of "Users", and another collection where there is an array of objects.
Something like this:
User
}
"_id": "61e6bbe49d7efc57f895ab50",
"name": "User 1"
},
{
"_id": "61e6b9239d7efc57f895ab02",
"name": "User 2"
},
{
"_id": "61cae6176d0d9a36efd8f190",
"name": "User 3"
},
{
"_id": "61cade886d0d9a36efd8f11a",
"name": "User 4"
},
The other collection looks like this:
{
users: [
{
user: {
"_id": "61e6b9239d7efc57f895ab02",
"name": "User 2",
},
...
},
{
user: {
"_id": "61cae6176d0d9a36efd8f190",
"name": "User 3",
},
...
},
],
},
I would like to get all the users in "array 1" excluding those in "array 2".
So the result should be:
[
{
"_id": "61e6b9239d7efc57f895ab02",
"name": "User 1"
},
{
"_id": "61cae6176d0d9a36efd8f190",
"name": "User 4"
},
],
So, is there a way to do that without the need to do aggregation.

you can use the $and like this:
const found = await User.find({
$and: [
{ _id: { $in: array1 } },
{ _id: { $nin: array2 } }
]
});

For anyone looking for a solution, if you want to add new element to an array of objects, where you, somehow, forgot to initialize the _id field, remember you have to, because mongo adds that field automatically (to use it for filtering/indexation).
All you have to do, is to write something like this;
const data = Data.create({
_id: mongoose.Types.ObjectId.createFromHexString(uid),
users: [{ user: userId, initiator: true, _id: userId}],
})
and for the searching, you write what you always write;
const found = await User.find({ _id: { $nin: data.users } });

Related

Populate Search MongoDB Objects

Can you please tell me how to search for a nested object (only _id appears in it and is expanded via .populate() )?
I need to search by title and album.title, how can I do this?
async getAll(searchTerm?: string) {
let options = {}
if (searchTerm) {
options = {
$or: [
{
title: new RegExp(searchTerm.trim(), 'gi'),
},
],
}
}
return this.TrackModel.find(options)
.select('-updatedAt -__v')
.sort({ createdAt: 'desc' })
.populate('album author')
.exec()
}
image of JSON below
[
{
"_id": "638cec330c055283f3eeb227",
"poster": "/uploads/tracks/Coolio/Coolio-cover.jpg",
"title": "Gangsta's Paradise",
"slug": "gangstasparadise",
"duration": 240,
"countPlays": 7269322,
"trackUrl": "/uploads/tracks/Coolio/Coolio - Gangstas Paradise.mp3",
"album": [
{
"_id": "638ceb960c055283f3eeb225",
"title": "Gangsta's Paradise",
"slug": "gangstasparadise",
"poster": "/uploads/tracks/Coolio/Coolio-cover.jpg",
"author": [
"638ce9f50c055283f3eeb223"
],
"createdAt": "2022-12-04T18:48:54.306Z",
"updatedAt": "2022-12-04T18:48:54.306Z",
"_v": 0
}
]
}
]
I tried to search using $or, but I ran into a problem that at the time of the search, only _id is stored in the album array.

Move existing outer document to inner document

Given a collection of documents similar to the following document
{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
}
where name is a unique field
How do I update all existing documents and add additional fields so it looks like this
{
"userDetails":{
"description": "janeetjack",
"name": "Pocog bistro janeetjack"
},
"userLikes":{
"likes": "food",
"plays": "ball"
},
}
You need to run set and unset on all docs with updateMany
Playground
db.collection.update({},
{
"$set": {
"userDetails.description": "$description",
"userDetails.name": "$name",
"userLikes.like": "abs"
},
"$unset": {
description: 1,
name: 1
}
})

Update recursive document in MongoDB

I have a recursive structure in one of my MongoDB collections, like so:
{
"id": String,
"date": Date,
"text": String
"replies": [{
// Same structure as above
}]
}
I need to update a document in this hierarchy by adding in a reply to a nested document. The guarantees are as follows:
The object to reply to is guaranteed to exist.
The path to the object to which a reply is to be posted is known (i.e., we have a sequence of the _id attributes to navigate).
The recursion depth is not bounded.
Searching SO gives me the following relevant questions:
Querying, 2013 - The recommendation was to switch to a graph DB.
Updating, 2015 - The recommendation was to use the $[] operator.
Based on the latter, my attempt was:
await commCollection.update(
{ _id: topLevel['_id'] },
{
$push: {
'replies.$[].$[comment_arr].replies': {
id: commentId,
text: comment,
date,
replies: []
}
}
},
{ arrayFilters: [{ 'comment_arr.id': responseTo }]}
);
where topLevel is the root document, responseTo is the id attribute of the object to add the reply to. This however, does not seem to work. What am I doing wrong, and how do I achieve this?
Update: An example below. Here's an example of a document from MongoDB Atlas:
{
"_id": {
"$oid": "605fdb8d933c5f50b4d2225e"
},
"id": "mr9pwc",
"username": "recrimination-logistical",
"upvotes": {
"$numberInt": "0"
},
"downvotes": {
"$numberInt": "0"
},
"text": "Top-level comment",
"date": {
"$date": {
"$numberLong": "1616894861300"
}
},
"replies": [{
"id": "dflu1h",
"username": "patrolman-hurt",
"upvotes": {
"$numberInt": "0"
},
"downvotes": {
"$numberInt": "0"
},
"text": "Testing reply level 1!",
"date": {
"$date": {
"$numberLong": "1618387567042"
}
},
"replies": [] // ----> want to add a reply here
}]
}
I've indicated where we want a reply added. responseTo in this case would be dflu1h.
Just need to remove $[] positional
Field name in arrayFilters must be an alphanumeric string beginning with a lowercase letter, Change comment_arr to any alphabets only like commentArr, remove special characters
await commCollection.update(
{ _id: topLevel['_id'] },
{
$push: {
"replies.$[commentArr].replies": {
id: commentId,
text: comment,
date,
replies: []
}
}
},
{ arrayFilters: [{ "commentArr.id": responseTo }] }
)
Playground

Update a subitem if it exists or insert it if not in mongodb

I'm new in here and I've just started to uses mongodb recently. I have a problem, I'm trying to update a collection's item in a given model if the item exists or update it if not.
I tried the following code but it's not working:
const res = await this.userModel.findByIdAndUpdate({
_id: user.id,
// 'devices.name': 'postman2'
}, {
$addToSet: { 'devices.[]': { name: 'postman2', generatedSecret: generatedSecret } }
}, { new: true, upsert: true }).exec();
My document looks like this:
{
"_id": {
"$oid": "5e9c6ffe9463db1594a74bec"
},
"email": "john.doe#mail.com",
"logins": [{
"provider": "email",
"secret": "sha1$e3d548b5$1$6f1b28e6b7cef47ca27b4e55ddbb6a5b8bc6b0ce"
}],
"devices": [{
"_id": {
"$oid": "5e9ddc9adb866666845bf86b"
},
"name": "postman",
"generatedSecret": "$2b$10$7fmneXGS1FjKyPXBa2Ea1erQfXF3ALjylIxOhetA9yxc3S95K4LVO"
}],
"applications": [{
"applicationId": "5e9c8b7c9463db1594a74bed"
}]
}
I want to update that devices item which matchs the query conditions. But if there isn't any matched result, then insert a new item with searched parameters.
Is possible to do this in one command?
Thanks!

search query on array elements in mongodb

I am new to mongodb and still learning it so my question can be naive so please bear with it :)
I have only one json object in mongodb which looks like this.
json object
{
"URLStore": [
{
"description": "adf description",
"url": "www.adf.com"
},
{
"description": "pqr description",
"url": "www.pqr.com"
},
{
"description": "adf description",
"url": "www.adf.com"
}
]
}
I need to query description for url which matches given input. e.g here www.adf.com . I have a code which queries mongodb
mongodb query
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("URLStore.url","www.pqr.com");
BasicDBObject fields=new BasicDBObject("URLStore.description", "");
cursor = collection.find(whereQuery,fields);
but the result is something like
{
"_id": {
"$oid": "554b4046e4b072dd9deaf277"
},
"URLStore": [
{
"description": "pqr description"
},
{
"description": "adf description"
},
{
"description": "adf description"
}
]
}
Actually only 1 description should have returned as matching objects with key www.pqr.com is only one. What is wrong with my query? m I missing something here ?
I have already tried question Retrieve only the queried element in an object array in MongoDB collection but using solution mentioned there will return only one object / first match
Use the following aggregation pipeline, should give you the desired results:
db.collection.aggregate([
{
"$match": {
"URLStore.url": "www.adf.com"
}
},
{
"$unwind": "$URLStore"
},
{
"$match": {
"URLStore.url": "www.adf.com"
}
},
{
"$group": {
"_id": {
"url": "$URLStore.url",
"description": "$URLStore.description"
}
}
},
{
"$project": {
"_id": 0,
"description": "$_id.description"
}
}
])