How can I get the value of this array
const { model, Schema} = require("mongoose")
let ticketSchema = new Schema({
GuildID : String,
MembersID: [String],
TicketID: String,
ChannelID: String,
Closed: Boolean,
Locked: Boolean,
Type: String,
Claimed: Boolean,
ClaimedBy: String,
Open:Boolean,
OpenBy:String,
})
module.exports = model("Ticket", ticketSchema);
Example:
{
_id: new ObjectId("63d8340e0a415466f7ad55f0"),
GuildID: '1066373193767592047',
MembersID:
[0]:811664013313310720
TicketID: '2',
ChannelID: '1069728239200780398',
Closed: false,
Locked: false,
Type: 'Hola',
Claimed: false,
Open: false,
OpenBy: '811664013313310720',
__v: 0
}
How can I get the value of MembersID:[0]:811664013313310720 in this case it would be 811664013313310720 I need it to check on a system
Try methods with findOne, Find
CodePudding user response:
I assume you use MongoDB Driver and the following syntax can extract the property MembersID
data = await dbo.collection(COLLECTION).find({},{projection:MembersID:1}).toArray()
MongoDB Drivers → Node.js
const MongoClient = require('mongodb').MongoClient;
const config = require('../config/mongodb'); // Your Own Connections Details
let db, dbo;
const COLLECTION = 'Ticket';
db = await MongoClient.connect(config.url, config.connection);
dbo = await db.db(config.database);
let data = [];
const options = {
projection: { MembersID: 1 }
};
data = await dbo.collection(COLLECTION).find({}, options).toArray();
data[0].MembersID // Access the Array
Mongoose
data = await dbo.collection('Ticket').find({}).exec();
Point to Note
The MembersID is a type of Integer, ensure you have right type when comparing
The MongoDB client connection details is omitted. MongoDB Connect to DB Official Document
Mongoose Refer to this document Mongoose find
CodePudding user response:
You can get the value of MembersID:[0] by using the Mongoose findOne method on the Ticket model and retrieving the MembersID field.
const Ticket = require("./path/to/Ticket");
Ticket.findOne({ GuildID: '1066373193767592047' }, (err, ticket) => {
if (err) {
console.error(err);
} else {
const membersID = ticket.MembersID[0];
console.log(membersID);
// Do something
}
});